我想通过在cakephp表上自动保存来实现拖放重新排序功能。
我正在尝试在我的CakePHP项目中实现此example,而不使用SQL命令。
我想更新表行的priority
字段。
因此在 view.ctp 中:
<div class="container">
<table class="table table-bordered">
<tr>
<th>#</th>
<th>Name</th>
<th>Defination</th>
</tr>
<tbody class="row_position">
<?php
foreach ($user->tasks_to as $task) {
?>
<tr id="<?php echo $task['id'] ?>">
<td><?php echo $task['id'] ?></td>
<td><?php echo $task['name'] ?></td>
<td><?php echo $task['description'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<script type="text/javascript">
$( ".row_position" ).sortable({
delay: 150,
stop: function() {
var selectedData = new Array();
$('.row_position>tr').each(function() {
selectedData.push($(this).attr("id"));
});
updateOrder(selectedData);
}
});
function updateOrder(data) {
$.ajax({
url:"updateTasksOrder",
type:'post',
data:{position:data},
success:function(){
alert('your change successfully saved');
}
})
}
</script>
并在控制器中:
public function beforeFilter(Event $event) {
parent::beforeFilter($event);
$this->Auth->allow();
}
public function updateTasksOrder()
{
$this->autoRender = false;
$position = $_POST['position'];
$i=1;
$connection = ConnectionManager::get('default');
foreach($position as $k=>$v){
$connection->update('tasks', ['priority' => $i], ['id' => $v]);
$i++;
}
}
但是它不起作用,它没有更新priority
字段。有什么建议吗?