Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails g:paginate - Passing search parameters back to Controller

I have built an advanced search option that allows users to search a multitude of different fields - 38 possible fields in total.

Once the initial search has been submitted, 10 results are displayed with pagination options at the bottom (using grails' <g:paginate> tag).

I think I have a few problems here.

1) I need to somehow pass the fields back to the controller using the params attribute of the g:paginate tag, but I don't really want to create a params map of 40 something parameters.

2) I pass back to the page from the Controller a map of search parameters (so that they can have a dismissable list of parameters that perform the search again without the dismissed parameter). I can pass this back in the params attribute, but it's passed back to the controller as a String and not a map so I don't know how to iterate through it (I realised this after it iterated through each separate character!).

3) The URL of the links that the g:paginate tag creates could potentially be massive, depending on how much of the search fields the user enters. Could there be a danger of the URL exceeding it's maximum amount?

I'm contemplating changing from the built in grails paginate functionality, and creating a form which I can POST. I'm not sure if this is the best way though, and I may be missing some alternative way to do this that is much better.

Any help greatly received!

like image 827
Gareth Lewis Avatar asked Feb 28 '14 11:02

Gareth Lewis


3 Answers

You can pass all your parameters to action using pageScope.variables like

<g:paginate params="${pageScope.variables}" total=.../>
like image 200
MKB Avatar answered Oct 20 '22 05:10

MKB


index.gsp

        <div class="pagination">

            <g:paginate total="${personCount ?:0}" max="5" params="${params}" />
        </div>

Controller.groovy

def index(Integer max)
{

    def c=Person.createCriteria()
    def results
    int count
    params.max = Math.min(max ?: 5, 100)
    if(params.name != null)
    {

         params.max = 4
         if(!params.offset) 
         {
             params.offset = 0;
         }
      count = Person.countByNameLike("%"+params.name+"%")
        results=c.list{
           like("name","%"+params.name+"%")
           maxResults(params.max)
           firstResult(params.getInt("offset"))
       }

    }
    else 
    {
        results = Person.list(params)
        count = Person.count()
    }

    respond results, model:[personCount: count]
}
like image 2
rstag Avatar answered Oct 20 '22 04:10

rstag


I'd put your search params into session. Thus you can use the very basic grails g.paginate tag w/o polluting it. Each time the user changes his search, the parameters should get updated.

like image 1
injecteer Avatar answered Oct 20 '22 05:10

injecteer