15

JsonRest ストアに接続されている dgrid にグリッドを作成しました。これにより、ピラミッド バックエンドからデータが読み込まれます。また、ストアに DnD 拡張機能を追加しました。DnD は機能しますが、行をドラッグしたときに意味のあるデータを送信する方法がわかりません。現在、GET と PUT の 2 つのリクエストを送信していますが、PUT には行からのデータだけが含まれており、データベース内の順序を更新するために使用できるものは何もありません。

新しい注文情報を取得するには、グリッドにどのような構成が必要ですか?

4

1 に答える 1

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++;
                });
            });

        }

位置を更新するために使用した PHP コードを次に示します。$fields格納するレコードを表す連想配列です。また、 と の 2 つの関数が存在することも前提としてい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;
    }
}

一度に複数のレコードを更新するクエリを使用すると、おそらくこれをより効率的にすることができますが、これの利点は、位置番号の予期しないギャップを適切に処理できることです。

于 2013-04-18T02:38:11.353 に答える