如何按多个字段对 web2py 网格行进行排序?

How to sort web2py Grid rows by multiple fields?

这里是 Web2py 新手。

给定下面的 Web2py 模型控制器和视图,我如何让 Web2py 在结果视图 (index.html) 中显示网格首先按 manufacturer 排序,然后按 commercial_name 排序?

型号:dy.py

db.define_table('item',
    Field('manufacturer', db.business_partner, label = 'manufacturer'),
    Field('commercial_name', required=True, label = 'commercial name'),
    Field('technical_data_sheet', 'upload', label = 'tds'),
    format='%(manufacturer)s %(commercial_name)s')

控制器:default.py

def item():
    grid = SQLFORM.grid(db.item)
    return dict(grid=grid)

查看:item.html

{{extend 'layout.html'}}
<h2>items and manufcaturer</h2>
{{=grid}}

使用 SQLFORM.grid()orderby 选项。您可以通过用“|”连接多个字段来对它们进行排序。

因此在您的情况下,网格定义将是:

grid = SQLFORM.grid(db.item, orderby=db.item.manufacturer|db.item.commercial_name)

阅读Grid Signature and orderby