如何在dgrid中使用dnd实现行重新排序

zef*_*ciu 15 javascript dojo drag-and-drop dgrid

我在dgrid中创建了一个连接到JsonRest存储的网格.这会从金字塔后端加载数据.我还在商店中添加了DnD扩展名.DnD工作,但我不知道如何在拖动行时发送任何有意义的数据.目前它发送两个请求,一个GET和一个PUT,但PUT只包含行中的数据,但我没有用来更新数据库中的顺序.

那么在我的网格中需要什么配置,以便我可以获得新的订购信息?

Ali*_*gji 3

这就是我所做的:

  1. position在数据库中维护一列
  2. 当一行被删除时调用 store.put,传递被删除项目的 id 和它被删除的位置。
  3. 更新位置时,相应移动其他项目

这是我的onDropInternal功能。它也可以处理删除多行:

        onDropInternal: function(nodes, copy, targetItem) {

            var store = this.grid.store, grid = this.grid, targetRow, targetPosition;

            if (!this._targetAnchor) return

            targetRow = grid.row(this._targetAnchor);
            targetPosition = parseInt(targetRow.data[grid.orderColumn]);
            responses = 1;

            nodes.forEach(function(node, idx){
                targetPosition += idx;
                var object = {id:grid.row(node).id};
                object[grid.orderColumn] = targetPosition;
                store.put(object).then(function() {
                        if (responses == nodes.length) grid.refresh();
                        else responses++;
                });
            });

        }
Run Code Online (Sandbox Code Playgroud)

这是我用来更新位置的 PHP 代码。$fields是一个关联数组,表示要存储的记录。它还假设存在两个函数:queryquery_row,我相信如果您选择使用此技术,您可以处理替换它们。

$table = "my_table";
$field = "position_field";
if (empty($fields['id'])) {
    //for new records, set the position field to the max position + 1
    $h = query_row("SELECT MAX(`$field`) as highest FROM $table LIMIT 1");
    $fields[$field] = $h['highest']+1;
} else if (is_numeric($fields[$field])) {
    //we want to move the row to $target_position
    $target_position = $fields[$field];
    //first get the original position
    $row = query_row("SELECT id,$field FROM $table WHERE id='$fields[id]' LIMIT 1");
    //start a list of ids
    $ids = $row['id'];
    //if the original position is lower than the target postion, set the incrementor to -1, otherwise 1
    $increment = ($row[$field] < $target_position) ? -1 : 1;
    //start a while loop that goes as we long as we have a row trying to take a filled position
    while (!empty($row)) {
        //set the position
        query("UPDATE $table SET $field='$target_position' where id='$row[id]'");
        //get the other row with this position (if it exists)
        $row = query_row("SELECT id,$field FROM $table WHERE id NOT IN ($ids) && `$field`='$target_position' LIMIT 1");
        //add it's id to the list of ids to exclude on the next iteration
        $ids .= ", ".$row['id'];
        //increment/decrement the target position
        $target_position += $increment;
    }
}
Run Code Online (Sandbox Code Playgroud)

您可能可以通过使用一次更新多个记录的查询来提高效率,但这样做的优点是它可以很好地处理位置编号中的意外间隙。