cakephp - CakePHP 3.6 TreeBehavior 结束 DataTables RowReorder 不保存新位置
问题描述
我需要将 Treebehavior(带有 parent_id、lft 和 rght 字段)与 DataTables Row Reorder 结合起来。我可以拖放行,但不会保存新位置。我应该如何保存“拖动”项目的新位置?
在我的控制器中,我有:
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() ?>
谁能告诉我需要添加什么来保存“拖动”项目的新位置?
解决方案
推荐阅读
- c - 在 gdb 中,我们可以将变量设置为表达式吗?
- java - 为什么选择数组列表
作为一个长而随机的字符串存储在 H2 数据库中? - mysql - 使用 custems 字段作为语句的两种不同海关邮政类型的 WP 查询
- javascript - TypeScript 中的 async/await 和 promise
- amazon-web-services - Amazon AWS:在本地下载亚马逊卷备份
- android - 吐司在 PictureInPicture 模式下看起来很奇怪
- cucumber - 如何绕过 Cypress + Cucumber 中的登录
- java - 是否有任何预定义的方法来交换两个元素?
- flutter - 如何在 Post Api Call inFlutter 中传递此类数据?
- java - 在哪里获得适用于 BusinessObjects 的 Java SDK