Adding a column of checkboxes to an SQLFORM.grid

别等时光非礼了梦想. 提交于 2019-12-11 06:27:27

问题


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.



来源:https://stackoverflow.com/questions/18062960/adding-a-column-of-checkboxes-to-an-sqlform-grid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!