I want to display all the project‘s tasks in a page, given that the user can have many projects. In such a case, a user will select any one project from the drop-down list box and I find that project instance using :
def proj = params.managersProject // this is the choice made by the manager
def project = Project.findByName("$proj")
Now I have all the details in project instance. Now I need to display the tasks of that particular project. Since it have so many tasks, I need to paginate it. But for paginating to work properly, I guess we need to pass the params like this :
Project.list(params) // which will be passed on to the views for rendering.
But in my case, I couldn’t able to call that list() method on project instance, that I got earlier. How do I solve this?
Edit:
After got answer from tim_yates, I tried this:
def tasks = Tasks.findAllByProject( project, params )
def size = tasks.size()
params.max = Math.min(params.max ? params.int('max') : 10,100)
return [tasksInstanceList: Tasks.findAllByProject(project,params), tasksInstanceTotal: size]
This returns the result, but when I click on the next button I don’t see any table being rendered. After that when I click on prev button, I couldn’t see anything. I clearly know that there is some problem with tasksInstanceList, I’m not sure whether the params is passing into it or not. But when I click on prev and next button I can clearly see my browsers add changing from :
http://localhost:8080/app/controller?offset=0&max=10
to:
http://localhost:8080/app/controller?offset=10&max=10
In view file I have this code:
<g:paginate total="${tasksInstanceTotal}"/>
Since I’m very new to grails, I need some help.
Thanks in advance.
I may not be the perfect expert on pagination (I typically trip a bit over it myself) but if its the project’s Tasks hasmany property you’re trying to paginate through, Project.list() probably isn’t what you want.
If your params has a .max and .offset (as in, you’re using the < g:paginate > tag and you provided it with a task listing controller action), what I typically do is use a criteriaBuilder with the somewhat-obscured PagedResultList return type. I’m guessing there’s a better way to go about it, but something like
Should put a list of params.max Tasks in your view’s taskList, but still give you the total number of tasks for that project in the taskCount variable.
Hope that helps!
Reference: http://blog.jeffshurts.com/2010/04/grails-pagination-and-criteriabuilder/
EDIT:
My guess is that what’s missing here is the action attribute for your paginate tag, especially if hitting the URL precisely is producing the proper results for you.
You need to point your paginate tag at whatever controller action has your code block.
One other thing I can think of looking at your controller code is your size variable might be misleading; it’s going to be the same as whatever params.max was. This is where the PagedResultList portion of my answer comes into play. Either that, or you could have size be equal to
That way your g:paginate tag’s total property will be accurate.