1. Go to this page and download the library: Download simialbi/yii2-nestable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
simialbi / yii2-nestable example snippets
use simialbi\yii2\nestable\widgets\Nestable;
use yii\helpers\Url;
use yii\web\JsExpression;
echo Nestable::widget([
'items' => [
[
'content' => '<div><a href="#test">My Item</a><span class="handle"></span></div>',
'options' => ['class' => 'list-group-item'],
'listOptions' => ['class' => 'list-group'],
'items' => [
[
'content' => '<div><a href="#testChild">My Child Item</a><span class="handle"></span></div>',
'options' => ['class' => 'list-group-item']
]
]
],
[
'content' => '<div><a href="#test2">My Item 2</a><span class="handle"></span></div>',
'options' => ['class' => 'list-group-item'],
'listOptions' => ['class' => 'list-group']
]
],
'clientOptions' => [
'expandOnHover' => 700,
'forcePlaceholderSize' => true,
'handle' => '.handle',
'isTree' => true,
'items' => 'li',
'placeholder' => 'placeholder',
'startCollapsed' => true,
'toleranceElement' => '> div',
// this js event will be called on change order of list
'relocate' => new JsExpression('function (evt, ui) {
var context = null;
var method = \'root\';
var parent = ui.item.parent(\'ul\').parent(\'.list-group-item\');
if (ui.item.prev(\'.list-group-item\').length) {
if (parent.length) {
method = \'after\';
}
context = ui.item.prev(\'.list-group-item\').data(\'id\');
} else if (ui.item.next(\'.list-group-item\').length) {
if (parent.length) {
method = \'before\';
}
context = ui.item.next(\'.list-group-item\').data(\'id\');
} else if (parent.length) {
method = \'prepend\';
context = ui.item.parent(\'ul\').parent(\'.list-group-item\').data(\'id\');
}
jQuery.ajax({
url: \''.Url::to(['site/my']).'/\' + method,
data: {
id: ui.item.data(\'id\'),
context: context
}
});
}')
]
]);