简体   繁体   中英

grails pagination of search results

I have domain "country" and on the list.gsp i have search block with input field. The first problem was when i tried to use paginate on my list always showed all result, in this case i fount the solution and i sent just 10 values for display (if you know another solution please tell me). My search looks this:

    def search = {
       if(query){
           def srchResults = searchableService.search(query, params)

           def result =  Country.executeQuery("select  new map(a.name as name, a.datacenter as datacenter) from Country a where a.name like '%"+ params.q + "%'")

           if (params.offset)
           {
               x = params.offset.toInteger()
               y = Math.min(params.offset.toInteger()+9, result.size()-1)
           } else
            {
            x = 0
            size = result.size() - 1
            y = Math.min(size, 9)
             }     
           def q=params.q

          [countryInstanceList: result.getAt(x .. y), countryInstanceTotal:result.size(), q:params.q]

       }else{
           redirect(action: "list")
       }
   }

Now i have another problem, when i press next page then my params from search field is cleaning and result is null. I tried to sent field value as parameter but i thing i do something wrong.

My search page looks like:

<g:form action="search">
            <div class="search" >
                Search Country
                <g:hiddenField name="q" value="${params.q}" />
                <input type="text" name="q" value="${params.q}" />
                <input type="submit" value="Search"/>
            </div>
        </g:form>

...... ......

The best solution I found:

for the action:

def list() {
... //use params to filter you query and drop results to resultList of type PagedResultList
return [resultList: resultList, resultTotal: resultList.getTotalCount(), filterParams: params]
}

for the view:

<g:paginate total="${resultTotal}" action="list" params="${filterParams}"/>

See a complete example .

If you have a very large number of rows in your table, paginating like this will break. Even on medium sized tables, it will be quite slow, since it loads in every row from the database.

A better solution is to do pagination in your query. You can pass in a map of query parameters for pagination as the optional third argument to executeQuery :

def maxResults = 10
def result = Country.executeQuery(
    "select  new map(a.name as name, a.datacenter as datacenter) from Country a where a.name like ?", 
    [params.q], 
    [max: maxResults, offset: params.offset])

Also, your form has two fields with name q . Don't use a hidden field with the same name as a text input. The default value for the text input can be specified with the value attribute.

And finally, you'll have to pass in the offset as a paramater. Grails has a tag that handles all this for you: g:paginate .

在params属性中添加params对象。

<g:paginate total="${resultTotal}" action="list" params="${params}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM