我在dgrid中创建了一个连接到JsonRest存储的网格。这会从金字塔后端加载数据。我还在商店中添加了DnD扩展名。 DnD工作,但我不知道如何在拖动行时发送任何有意义的数据。目前它发送了两个请求,一个GET和一个PUT,但PUT只包含行中的数据,但我没有用来更新数据库中的顺序。
那么在我的网格中需要什么配置,以便我可以获得新的订购信息?
答案 0 :(得分:3)
这就是我的所作所为:
position
列这是我的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
是表示要存储的记录的关联数组。它还假设存在两个函数:query
和query_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;
}
}
您可以通过使用一次更新多个记录的查询来提高效率,但这样做的好处是可以很好地处理位置数中的意外差距。