Example with sequential numbering
Totum has a unique row sorting sequence — id, but sometimes additional sorting is needed, for example, if the table includes not only adding but also deleting rows or if there are restarts/cancellations of transactions.
id values are not reused, so gaps appear.
This is most often needed in calculation tables within a cycle — for example, to pass sequential numbers to accounting or for printing receipts.
We create a number field and write Code in it that will calculate the number:
= : if(condition: $sel = ""; then: 1; else: $calc)
~sel: select(table: $#ntn; field: $#nf; where: 'id' < $id; order: 'id' desc)
id: if(condition: #id = ""; then: "*ALL*"; else: #id)
calc: $sel + 1
In this example, we check for the existence of the previous number and also check id for add row. If it is an add row, we take the last row by id in the table; if not, we take the first row with an id less than the current one.
n-sorting example
If the parameter sort by order field is enabled, then the same needs to be done by n instead of id:
= : if(condition: $sel = ""; then: 1; else: $calc)
~sel: select(table: $#ntn; field: $#nf; where: 'n' < $n; order: 'n' desc)
n: if(condition: #n = ""; then: "*ALL*"; else: #n)
calc: $sel + 1