Hi. I need to add field (column) to the SQLFORM.grid. The column is simple order number – just numbers 1,2,3,4,... at the beginning of every row.
I did not find a builtin feature of grid to make that, but can think of two ways of doing this
a) Using virtual fields and the grid fields argument.
b) Using helper methods for customizing the table
I think b) is more simple to code but can have less performance than a)
b)
grid = SQLFORM.grid(query or table)
trs = grid.element("table").elements("tr")
grid.element("thead").insert(0, TH(<name>))
for i, tr in enumerate(grid.element("tbody").elements("tr")):
tr.insert(0, TD(i+1))
a) is just about setting a virtual field for the table with a record counter and specifying it in the grid fields argument .
EDIT: It should be enough to add the virtual field without using fields in .grid, unless you want to filter columns.
Also, as you can see, this field should NOT be viewed in update or create mode.
Perhaps you could read the request arguments so you can enable/disable the row count when there was a record edit/update requested.