我有一个看起来像这样的方法
def searchInAllField(String searchString) {
Long companyId = sessionService.userProfile.companyId;
return Vendor.createCriteria().list(){
eq("companyId", companyId);
if (searchString) {
or {
ilike("name", "%${searchString}%")
ilike("businessName", "%${searchString}%")
ilike("address", "%${searchString}%")
ilike("description", "%${searchString}%")
ilike("contactPerson", "%${searchString}%")
ilike("mobileNumber", "%${searchString}%")
ilike("officeNumber", "%${searchString}%")
ilike("email", "%${searchString}%")
}
}
order("businessName", "asc");
} ?: [];
}
此方法接受searchString参数,并检查它是否与所有字段中的值匹配。
此方法的结果是一个列表,需要显示该列表并进行分页。如果我已经有了列表,如何进行grails分页?
最佳答案
要使用分页,您的searchInAllField()
方法需要合并max
和offset
参数。
def searchInAllField(String searchString, int max, int offset) {
Long companyId = sessionService.userProfile.companyId;
return Vendor.createCriteria().list(max: max, offset: offset) {
eq("companyId", companyId);
if (searchString) {
or {
ilike("name", "%${searchString}%")
ilike("businessName", "%${searchString}%")
ilike("address", "%${searchString}%")
ilike("description", "%${searchString}%")
ilike("contactPerson", "%${searchString}%")
ilike("mobileNumber", "%${searchString}%")
ilike("officeNumber", "%${searchString}%")
ilike("email", "%${searchString}%")
}
}
order("businessName", "asc")
}
}
调用
searchInAllField()
的 Controller 方法将需要传递max
和offset
并提供模型中的总数:def someControllerMethod() {
def vendors = whatever.searchInAllField(searchString, params.max, params.offset)
render(model: [vendors: vendors, totalVendors: vendors.totalCount])
}
总计数(在这种情况下为
vendors.totalCount
)很重要。 g:paginate
taglib需要它才能正常运行。注意:g:paginate文档可能会误导您。它演示了使用
DomainClass.count()
作为总数。您需要的是查询结果计数,而不是数据库表中的记录总数。 totalCount
属性提供了您所需的内容,但是只有在使用offset
和max
执行查询时,您才可以获取该属性。最后,您可以在GSP代码中使用
g:paginate
taglib呈现分页链接:<g:paginate controller="someController" action="someControllerMethod" total="${totalVendors}" />
关于grails - 如果已提供列表,如何使用grails分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36148995/