CakePHP 3.6 TreeBehavior end DataTables RowReorder не сохраняет новую позицию - PullRequest
0 голосов
/ 01 сентября 2018

Мне нужно объединить поведение дерева (с полями parent_id, lft и rght) с переупорядочением строк в DataTables. Я могу перетаскивать строки, но новые позиции не сохраняются. Как сохранить новую позицию «перетаскиваемого» элемента?

В моем контроллере у меня есть:

    public function index($archived = 0)
{
    // Row Reorder
    $groups = TableRegistry::get('Groups');
    if ($this->request->is('ajax')):
        $this->autoRender = false;
        if (!empty($this->request->data)):
            $oldPosition = $this->request->data['oldPos'];
            $newPosition = $this->request->data['newPos'];
            $nodeId = $this->request->data['id'];

            $diff = abs($oldPosition - $newPosition);

            $node = $groups->get($nodeId);
            for ($i=1;$i<=$diff;$i++):
                if ($oldPosition > $newPosition):
                    $groups->moveUp($node);
                else:
                    $groups->moveDown($node);
                endif;
            endfor;
        endif;
    endif;
    // set archived condition
    if ($this->request->getQuery('archived')):
        $conditions['Groups.archived'] = $this->request->getQuery('archived');
        if ($this->request->getQuery('archived') == 1):
            $this->set('switch_background_color', SWITCH_ARCHIVE_BACKGROUND_COLOR);
        endif;
    else:
        $conditions['Groups.archived'] = 0;
    endif;
    $groups = $this->Groups->find('all', [
        'conditions' => $conditions,
        'order' => ['Groups.lft' => 'ASC']
    ]);
    if (empty($groups)):
        $this->Flash->error(__('No {0} yet!', __('Groups')) . ' ' . __('Please add one.'), ['params' => ['class' => 'warning', 'escape' => false]]);
        $this->redirect(['action' => 'add']);
    endif;
    // count archived
    $archived = $this->Groups->find('all', [
        'conditions' => ['Groups.archived' => 1]
    ]);
    $count_archived = $archived->count();
    $this->set(compact('groups', 'count_archived'));
}

И в моем индексном представлении у меня есть:

<table id="datatable-<?= $controller ?>">
    ...
</table>

<?= $this->Html->scriptStart(['block' => 'scriptBottom']) ?>
    var table = $('#datatable-<?= $controller ?>').DataTable({
        rowReorder: true,
        paging: false,
        columnDefs: [
            {targets: 0, visible: false},
            {targets: 1, visible: false},
            {targets: 2, orderable: false},
            {targets: 3, orderable: false},
            {targets: 4, orderable: false},
            {targets: 5, orderable: false},
            {targets: 6, orderable: false}
        ]
    });

    table.on('row-reorder', function (e, diff, edit){
        var oldPos = "";
        var newPos = "";
        var group_id = edit.triggerRow.data()[1];

        for (var i=0, ien=diff.length; i < ien; i++){
            var rowData = table.row(diff[i].node).data();
            if (edit.triggerRow.data()[1] == rowData[1]){
                oldPos = parseInt(diff[i].oldPosition + 1);
                newPos = parseInt(diff[i].newPosition + 1);
            }
        }

        jQuery.ajax({
            url     :   'groups',
            type    :   'POST',
            data    :    {'id':group_id,"oldPos":oldPos,"newPos":newPos},
            success: function(res){

            }
        });
    });

    $('div.dataTables_filter input').focus();
<?= $this->Html->scriptEnd() ?>

Может кто-нибудь сказать мне, что мне нужно добавить, чтобы сохранить новую позицию «перетаскиваемого» элемента?

...