如何在Grails框架中动态设置createCriteria的where过滤器



我正在groovy中使用分页进行搜索。为了在不同的页面中分页,我在其中放置了一个计数过滤器。我想动态地得到colName但对于where过滤器,我们需要放一个域实例。这里的域是Release。还有其他的计算方法吗?

def search(Integer max, Integer offset) {                                                                                                                 
def searchText = params.searchText                                                                                                                    
def colName = params.colName                                                                                                                          
def ReleaseList                                                                                                                                  
def ReleaseCount                                                                                                                                 
params.max = params.max ? params.int('max') : 10                                                                                                      
if (searchText) {                                                                                                                                     
def rel = Release.createCriteria()                                                                                                           
List<Release> releasesList = rel.list() {                                                                                                    
eq(colName, searchText)                                                                                                                       
} as List<Release>                                                                                                                           
ReleaseList = releasesList                                                                                                                   
ReleaseCount = Release.where {                                                                                                          
colName == searchText                                                                                                                         
}.count()                                                                                                                                         
} else {                                                                                                                                              
ReleaseList =  Release.list(params)                                                                                                      
ReleaseCount = Release.count()                                                                                                          
}                                                                                                                                                     
render(template: 'grid', model: [ReleaseInstanceList: ReleaseList, ReleaseInstanceCount: ReleaseCount], searchText: searchText)   

}

如果你为list()方法传递max参数,它将返回一个PagedResultList实例,这是一个围绕项目的包装器,也包含匹配项目的totalCount

PagedResultList releases = Release.where { colName == searchText }.list(max: 10)
int totalCount = releases.totalCount

这也适用于CreateCriteria

最新更新