searchgroovypaginationwhere-clausecreatecriteria

How to make where filter of createCriteria dynamic in Grails framework


I am doing search with Pagination in groovy For the sake of pagination in different pages, I put a count filter "where". I want to get colName here dynamically but for where filter we need to put a instance of the domain. Here, the domain is Release. Is there any other way to calculate count?

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)   

}


Solution

  • If you pass in the max parameter for the list() method it will return a PagedResultList instance, which is a wrapper around the items that also hold the totalCount of matching items.

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

    This also works for CreateCriteria.