How do I structure my pages and partial templates so that Ajax will play nice with <paginate> and column sorting?
I currently have a search.gsp page with a remoteField that calls a controller to update a template. This all works fine. However, the column sorting and paging actions cause my search.gsp to be completely replaced by the template view.
From my search.gsp:
<div id="searchBox">
Enter a key or phrase: <g:remoteField name="searchBox"
update="resourceSearchResultPanel" paramName="q"
url="[controller:'resourceEntry',action:'searchForResources']"
/>
</div>
<div id="resourceSearchResultPanel" />
My controller handles the search request like so:
def searchForResources = {
params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
params.offset = params.offset ? params.offset.toInteger() : 0
log.debug "Handling search post action"
def q = params.q ?: null
log.debug "Search phrase is $q"
def searchResults
if (q) {
searchResults = [
results: ResourceEntry.search(q,[offset: params.offset, max: params.max]),
resultCount: ResourceEntry.countHits(q),
q: q.encodeAsHTML()
]
}
render(template:"resourceSearchResultPanel",model:searchResults)
}
The _resourceSearchResultPanel.gsp is just a table with this <paginate> tag:
<g:paginate action="searchForResources" total="${resultCount}" params='["q":"${q}"]' />
The problem is that when the <paginate> tag calls the controller, the entire page is refreshed with the contents of the _resourceSearchResultPanel.gsp template, while I just want the _resourceSearchResultPanel.gsp itself to be refreshed inside search.gsp.
There's no update attribute like there is in the remoteField tag...
The paginate tag doesn't support generating ajax links so you'll have to write your own verison of the tag that calls remoteLink instead of link.
cheers
Lee
I would suggest to you remote pagination plugins for Grails. This would suffice your requirement. For more details please refer to following site:-
http://www.grails.org/plugin/remote-pagination
Please feel free to revert incase of any concern.
Related
This is the code I am using in the gsp file to fetch the data to show on the view page:
<datePicker id="startDate" name="startDate" value="${new
Date().minus(2).format('yyyy-MM-dd')}" />
the datepicker I am using in the same page.
Now i need to pass the datepicker parameters to this link
<a id="exportIcon" href="${createLink(controller: entityName, action:
'mrInventoryExcelExport', params: [StartDate:startDate])}" >
the parameter is the startdate which i enter manually in the form
Can anyone tell me how can i achieve this.
You could just use a button & submit the form to the given action then either deal with it in the action directly:
gsp:
<g:actionSubmit name="exportIcon"
action="mrInventoryExcelExport"
value="Export"/>
controller:
def mrInventoryExcelExport() {
def startDate = params.startDate
...
}
Or redirect to another action with the startDate:
gsp:
<g:actionSubmit name="exportIcon"
action="anotherAction"
value="Export"/>
controller:
def anotherAction() {
redirect( controller: 'entityName', action: 'mrInventoryExcelExport', params:[startDate: params.startDate] )
}
You need to understand the mechanics. GSP is a server-side technology. Whatever related you have in there, will be processed and converted to HTML, before it is sent to the client/browser.
Now, what you're asking to include/change a parameter in the link, based on the value picked by user; mind you, the link is already created. No chance? Using JavaScript to create that link is your best bet.
P.S.: Try to see the page source from the browser, for more insight.
I am working with Grails 2.4.2. I have a view page named distGameList where I am showing the distributed game list. So when the list is over than 10 it should place other values in next page. But It's not doing it. In-fact It is creating 2 pages. I have 12 rows in that table. Now in view page 2 page is showing that's OK. But 12 rows is showing each pages although it should show 10 rows at first page and 2 rows in second page. Here are my steps below ::
in my controller ::
def distGameList(){
def distributedGameList = AndroidGameDist.getAll()
[distributedGameListInstance : distributedGameList, androidDistGameInstanceCount: AndroidGameDist.count()]
}
and in my view where paginate tag after ending table tag ::
<div class="pagination">
<g:paginate total="${androidDistGameInstanceCount ?: 0}" />
</div>
Use list for pagination and pass params in it.
def distributedGameList = AndroidGameDist.list(params)
or
Use this for more readable code:
def distributedGameList = AndroidGameDist.list(max: params.max ?: 10, offset: params.offset ?: 0)
I want to display the results of a queries on several pages, so I use the tag pagination.
I have correct outputs (I want 5 result per page) in all the pages but the first one.
In the first page, all results are displayed.
Controller :
def questions(){
def questions = Question.list(params)
[questions: questions, total: Question.count()?:0, tags: Tags.list(), params: params]
}
View
<g:each in="${questions}" var="question">
<div class="row">
${question?.body }
</div>
</g:each>
<div class=pagination>
<g:paginate controller="Question" action="list" total="${total}" max="5" params="${params}"/>
</div>
Why do I have all results in the first page ?
This happens because params has no max value.
setting params.max = 10 and then invoking questions() would result in 10 items for the first page.
// will result in Question.list(max: 10)
def questions = Question.list(params)
But, make sure if same action is called again for subsequent pages then params.max has to be set to 5 or totally removed because max from <g:paginate> would take care of the rest.
You can add following line at the beginning of your controller action:
params.max = params.max ?: 5
If no max parameter is given the default value will be 5.
My controller is the folowing:
def participated = {
def temp = ConferenceUser.get(params.temp)
def prizes = Prizes.findAllByConferenceUser(temp) // find all rooms where current computer is
def subms = Submissions.findAllByConferenceUser(temp) // find all rooms where current computer is
[temp: temp, priz: prizes, subm: subms]
}
But somehow, when I successfully update a conference value, I wanna go back to the initial page (participated) but I don't know how to pass back the params.temp. (if I do a simple redirect, as the controller is expecting params.temp, it will give me an error because I cannot search prizes with a null object as parameter. So, imagine my update controller is the following:
def update = {
def saveParamshere = params.temp
...
...
(code here)
...
...
redirect(action: "participated", params: [temp: saveParamshere])
}
This code isn't working. How can I successfully go back to my main page and pass in params.temp ?
I think the problem may be, that you are calling update action by submitting form (I suppose). Maybe you are not passing temp value from that form? You can do it by embedding temp as hidden input field into form, or apply it to url by param attribute on form tag.
Using hidden field it might be something like this (in your view file):
<g:form controller="somecontroller" action="update">
(...)
<g:hiddenField name="temp" value="${temp}" />
(...)
</g:form>
Using params attribute:
<g:form controller="somecontroller" action="update" params="[temp : temp]">
(...)
</g:form>
I didn't test any of these so there might be some issues, especially in the second approach.
You could put the params in the flash scope, which lives for two requests, or put them in the session and retrieve them that way.
Here is a link to the grails docs on usage of flash scope:
Grails - Controllers - Controller Scopes
I have a Groovy application. I am rendering the view list using the following statement:
render (view: 'list', model:[reportingInstanceList: reportingInstanceList, reportingInstanceTotal: i, params: params])
The list.gsp is as follows:
The view is rendered but the default sorting is not working.
<g:sortableColumn class="tabtitle" property="id" title="Id" titleKey="reporting.id" />
<g:sortableColumn class="tabtitle" property="company" title="Company" titleKey="reporting.company" />
Unfortunately the default sorting (by id, by company, etc) are not working.
Any hint why?
Thanks a lot in advance.
Luis
If you are asking about the sorting/order links at the top of the columns on the list page, the links are hrefs back to the controller and method that was originally used to populate the list. Plus the URLs include parameters for sort and order. For instance:
/tracker/bug/searchCurrentUserProject?sort=name&order=asc
The controller method will then need to handle the sort and order values from the link:
params.sort = params.sort ?: "priority"
params.order = params.order ?: "asc"
And pass them to the database query:
def bugList = Bug.createCriteria().list(
sort:params.sort,
order:params.order,
max:params.max,
offset:params.offset) {
eq "projectId", new Integer (params.projectId)
}
You can add two hidden fields in your form, set them dynamically with Javascript and send them together with your form.