您可以向 SQLFORM.grid 添加自定义字段/列吗? [英] Can you add a custom field/column to a SQLFORM.grid?
问题描述
我正在尝试使用 SQLFORM.grid()
一切正常,但我正在尝试向其中添加一个新的 field
.我的查询由两个连接在一起的表组成.例如:
I am trying to use a SQLFORM.grid()
everything works find however I'm trying to add a new field
to it. My query is made up of two tables joined together. For example:
db.define_table('table1',
Field('total','integer')
)
db.define_table('table2',
Field('table1_id','integer'),
Field('count','integer'),
)
query = (db.table1.id == db.table2.table1_id)
grid = SQLFORM.grid(query,fields=[
db.table1.total,
db.table2.count,
(db.table1.total - db.table2.count) #this doesn't work
])
那么是否可以添加一个通过数学函数生成的列,或者在这种情况下不一定需要但两个值连接在一起?
So is it possible to add a column that is generated via a math function or not necessarily needed in this case but two values concatenated together?
推荐答案
您可以使用links"参数:
You can use the "links" argument:
grid = SQLFORM.grid(query, fields=[db.table1.total, db.table2.count],
links=[dict(header='Total - Count',
body=lambda r: r.table1.total - r.table2.count)])
默认情况下,所有链接"列都显示在右侧.您可以通过将links_placement"参数设置为left"或both"来更改它.
By default, all "links" columns appear on the right. You can change that by setting the "links_placement" argument to either "left" or "both".
这篇关于您可以向 SQLFORM.grid 添加自定义字段/列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!