gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:34:48 25 4
gpt4 key购买 nike

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

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

最佳答案

这是我的做法:

  1. 在数据库中维护一个位置
  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++;
});
});

}

这是我用来更新位置的 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;
}
}

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

关于javascript - 如何在 dgrid 中使用 dnd 实现行重新排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14796968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com