Adding a column of checkboxes to an SQLFORM.grid

2019-07-24 16:45发布

问题:

I'd like to have a grid in which the first column contains no header, and consists of checkboxes, the last couple of columns are columns of links (generated with the 'links' parameter), and the intermediate columns are generated from database fields with the 'fields' parameter. What's the best way to go about generating the column of checkboxes? Thank you.

回答1:

grid = SQLFORM.grid(..., selectable=lambda ids: [do something with record ids])

That will add a column of checkboxes on the left and a "Submit" button at the bottom of the grid. When the "Submit" button is clicked, the action that generated the grid will receive a list of record IDs for the checked records, and those IDs will be passed to the "selectable" argument (which should be a callable that takes a list of record IDs, as above).

You can control the label of the submit button and even add additional functions to apply to the checked records by passing a list of lists/tuples as the "selectable" argument:

grid = SQLFORM.grid(...,
    selectable=[('Action 1', lambda ids: [do action 1 with ids], 'class1'),
                ('Action 2', lambda ids: [do action 2 with ids], 'class2')])

In that case, at the bottom of the grid you will get buttons labeled "Action 1" and "Action 2", and the appropriate action will be executed depending on which button is clicked. The third element in each tuple is an optional CSS class that will be added to the button element of that action.



标签: web2py