Skip to content

Order index (renumber rows after a move) ​

Since 1.6.0

orderIndex and renumberOrder, getOrderGaps and nextOrderValue were added in 1.6.0.

Many screens store an order number (seq) on the server. Sending a new number for every row after each drag is heavy, and updating only the moved row means working out "a number between its neighbours" yourself. With orderIndex on, the grid picks and writes the numbers of the moved rows for you. It is off by default.

Try it in the gallery example Order numbers follow the moved rows.

Turn it on ​

ts
createGrid(el, {
  columns: [{ id: 'name' }, { id: 'seq', title: 'Order' }],
  rows, // seq: 10, 20, 30 ...
  rowKey: 'id',
  rowDrag: true,
  orderIndex: { field: 'seq' },
});
OptionDefaultDescription
field(required)The row field that holds the order number
step10Gap between numbers (integer, 1 or more)
startstepFirst number when everything is renumbered
renumber'moved''moved' renumbers only the moved rows, 'all' renumbers everything on every move

A wrong option throws invalid-option when the grid is created.

How numbers are chosen ​

When rows move (moveRows, moveRowsBy, indentRows, outdentRows or a drag and drop), the grid writes the field of the moved rows.

  • The midpoint of the two neighbours: moving between 10 and 20 gives 15. Several rows moved as one block share the gap evenly.
  • At the very top the number is the next one minus step, at the very bottom the previous one plus step.
  • If the neighbours are less than 2 apart and nothing fits between them, all rows with the same parent are renumbered start, start + step, ... (renumber: 'all' always renumbers them all).
  • In a tree only siblings under the same parent are compared. When indent or outdent changes the parent, the row gets its number among the new siblings.
  • Cells that are not numbers (empty or text) do not take part. A neighbour that is not a number counts as missing, like the very top or bottom.
  • Numbers always follow the data order, not the sorted or filtered view. Moving rows is already blocked while sorting, filtering or paging.

The write is one updateRows call. Changed rows are tracked as modified, and the move together with the number write is one undo step.

Use it from code ​

ts
api.renumberOrder(); // renumber everything per parent, returns the number of changed rows
api.renumberOrder({ field: 'seq', step: 100, start: 0 });

api.getOrderGaps();
// { field: 'seq', min: 10, max: 90, minGap: 1, needsRenumber: true }

// the number for a new row
const seq = api.nextOrderValue({ after: 3 }); // 'start' | 'end' | { before: key } | { after: key }
api.addRows([{ id: 99, name: 'New task', seq }], { index: 3 });
APIWhat it does
renumberOrder(opts?)Renumbers to start, start + step... and returns the number of changed rows. One undo step
getOrderGaps()Smallest and largest number, the smallest gap between neighbours, and whether a renumber is needed (needsRenumber)
nextOrderValue(position)Works out the number for a new row without changing anything. Returns a fraction such as 10.5 when no whole number fits

Without the orderIndex option you can still pass the field yourself, as in renumberOrder({ field }). With neither, it throws invalid-option.

Event and hook ​

ts
api.on('orderIndexChanged', ({ rowKeys, field, renumbered }) => {
  // rowKeys: rows whose number changed, renumbered: whether everything was renumbered
  saveToServer(rowKeys.map((key) => api.getRowByKey(key)));
});

createGrid(el, {
  // ...
  hooks: {
    beforeOrderIndexChange: ({ changes, renumbered, source }) => {
      if (changes.length > 200) return false; // cancels only the number write (the move stays)
    },
  },
});

changes is a list of { rowKey, from, to }. source is 'move' after a row move and 'api' for renumberOrder(). To save on a server, send just the rows from orderIndexChanged.