What is wrong with this code, because im getting all the results returning to the view, which takes me too much to load. I want it to return only 5 per page.
Controller:
{
def channel2 = Channel2.list(params)
//def prog = Programmation.list()
def prog
def progs = [:]
def temp
channel2.each{
Programmation p = Programmation.withCriteria {
eq('prog_channel', it)
'between'('prog_start', new Date(), new Date() +1)
}
progs.put(it.id, p)
}
[channel: channel2, program: progs]
}
GSP
<g:render id="" template="/layouts/canais"/>
<g:paginate next="Forward" prev="Back"
maxsteps="0" max="3" controller="teste"
action="myProgramms_canais"
total="${tv_megazineplus.Channel2.count()}" />
I cann't figure it out. I followed Grails helpPage, it should be working.
Solution: The first time the that action is called, it loads all channels because the params.max is set to null. To fix that, just use:
def offset
if(!params.offset){
offset = 0
}
def channel2 = Channel2.list(max:5, offset: offset)
Related
i am trying to use cookie in grails 3.
i tried this plugin but i don't know why its not work at all..
cookieService.setCookie('username', customer?.email)
and i use this code for call it from gsp
<g:cookie name="username"/>
i also tried this way..
def cokusername = cookieService.setCookie('username', customer?.email)
println "cookieService.getCookie('username') = "+cookieService.getCookie('username')
redirect(controller: "toko",cokusername: cokusername)
and this is in my tokoController.groovy index :
def index={
def toko = CifLogo.executeQuery("from CifLogo order by rand()",[max: 10])
// def itemRandom = Item.executeQuery("from Item where cif = :cif order by rand()",[max:12,cif:cif])
def awdf = cookieService.getCookie('username')
println "awdf = "+awdf
println "cokusername = "+params.cokusername
[tokoList:toko,cokusername:awdf]
}
i have no idea to retrieve my cookie. :(
update
def index(){
def toko = CifLogo.executeQuery("from CifLogo order by rand()",[max: 10])
// def itemRandom = Item.executeQuery("from Item where cif = :cif order by rand()",[max:12,cif:cif])
def awdf = cookieService.getCookie('username')
println "awdf = "+awdf
println "cokusername = "+params.cokusername
[tokoList:toko,cokusername:awdf]
}
i tried to print cookie like this..
def awdf = request.getCookie('username')
println "awdf = "+awdf
println "cokusername = "+params.cokusername
request.cookies.each { println "${it.name} == ${it.value}" }
and this is what the result
From what I can see this line:
redirect(controller: "toko",cokusername: cokusername)
Should be:
redirect(controller: "toko",params:[cokusername: cokusername])
Also actions using closures in grails 3 will have undesired results. You should change to methods. Hence this line:
def index={
SHould be:
def index(){
Apart from this it seems the cookieService code should work fine, so I can only assume its being caused my the closure index that should be a method.
Another thing could be the fact that you are doing a redirect, which will clear the request and not persist any cookies that were set before the redirect
I don't know why, but maybe it's a bug.
i use this code to setCookie
cookieService.setCookie(name:"username", value: customer?.email, maxAge: 24*60*60, path: "/")
after read this code.
and i cannot deleteCookie with this code.
cookieService.deleteCookie(cookieService.findCookie("username"))
because when i print cookieService.findCookie("username") it returns javax.servlet.http.Cookie#78cbf320
and method deleteCookie(Cookie cookie) from this link
so i think it mustbe deleted.
but still availlable.
so i can answer this question about setCookie not deleteCookie
i also tried this way to delete cookie..but still failed.
CookieService.setCookie(name:"username", value: "", maxAge: 0, path: "/")
This is my gsp view where I have to show the pagination but I am not getting any pagination and below that I've printed the job count and I am getting the correct job count.
<div class="paginate">
<g:paginate total="${total}" maxsteps="1" next="Forward" prev="Back" controller="job" action="viewJobs"/>
</div>
Job Count======${total} //Just for check
My controller code :
def viewJobs() {
User userEmail = springSecurityService.getCurrentUser()
List<Job> list1 = Job.findAllByCompany(userEmail.company,[max:5])
println "******list1*******" + list1
render(view: 'viewJob', model: [jobs: list1,total:list1.size()])
}
Your action should look like
def viewJobs(Integer offset) {
User userEmail = springSecurityService.getCurrentUser()
List<Job> list1 = Job.findAllByCompany(userEmail.company,[max:5,offset:offset])
println "******list1*******" + list1
render(view: 'viewJob', model: [jobs: list1,total:Job.countByCompany(userEmail.company)])
}
Pagination works with offset parameter (just like in SQL) and totalCount should be total number of records in DB not the size of the list(which will be always less than or equal to 5 in your case)
I'm trying to use the paginate tag in grails but it isn't working.
in controller:
def show(Integer max) {
params.max = Math.min(max ?: 10, 100)
def etpse
def total
if (params.data == 'all') {
etpse = Enterprise.findAll()
total = Enterprise.count()
}
else {
def paramsLike = "%" + params.data + "%"
etpse = Enterprise.findAllByKeywordLike(paramsLike)
total = Enterprise.countByKeywordLike(paramsLike)
}
[etpseList: etpse, instanceTotal: total]
}
in gsp:
<div id='pagination'>
<g:paginate total="${instanceTotal}" />
</div>
The paginate tag doesn't filter the results in your page, nor does it render the list of items. It merely creates links for the next/previous pages based on your request's parameters.
Your controller is responsible for fetching the correct page of data and your gsp is responsible for rendering the actual list of items.
The paginate tag parameters are designed to match the parameters to the GORM-injected list method and almost always go hand-in-hand:
class ItemController {
def list() {
[items: Item.list(params), itemCount: Item.count()]
}
}
view:
<g:each var="item" in="${items}">
<!-- render items here -->
</g:each>
<g:paginate controller="item" action="list" total="${itemCount}"/>
In the above code, the params list (including things like max and offset) is passed to the list method of the Item domain class, and this will grab a single page of data.
The paginate tag examines the request parameters for the same entries, determines which page of data you're viewing and creates the necessary links to the next and previous pages by using the correct values for max and offset.
Here you go.
def show(Integer max) {
Integer offset = params.int("offset")
Integer max = Math.min(params.int("max") ?: 10, 100)
if (params.data == 'all') {
params.data = '%';
}
def c = Enterprise.createCriteria()
def results = c.list(max: max, offset: offset) {
ilike('keyword', "%" + params.data + "%")
}
[etpseList: results, instanceTotal: results.totalCount]
}
You have to pass your params max and offset into your findAll, otherwise Grails does not know how to paginate your resultset.
For example,
Book.findAll(query, [max: 10, offset: 5])
The ability to use Pagination is the goal, however I'm not sure how to work with this. The relationship is unidirectional hasMany (seen below). As current it works fine without pagination (see Screenshot), but to work with pagination I have to change the controller to use createCriteria. This is where I come into difficulty as its quite strange for the relationship.
Domain
Class Tag {
String Tag
User user
static hasMany = [events: Event]
}
Controller
def searchTags() {
println("TAG CONTROLLER - Params sent on click of tag are: "+params.selected)
def results = Tag.findByTagAndUser(params.selected, lookupPerson())
//resultTotal is for show for the time being
[query: params.selected, results: results, resultTotal: 0]
}
GSP
<g:each in="${results.events}" status="i" var="t">
.... ommited as unnessary
</g:each>
//Not working yet due to confusing createCriteria issue
<div class="pagination">
<g:paginate total="${resultTotal}" />
</div>
I could do with a little guidance, this is what I'm thinking so far but I'm a little confused:
def tagCriteria = Tag.createCriteria()
def result = tagCriteria.list (max: 50, offset: 10) {
eq("tag", params.selected)
and {
eq("user", lookupPerson())
}
//Not sure what to do at this point compared with the findByTagAndUser(params.selected, lookupPerson()).events events being the hasMany relationship seen in domain
events {
}
}
Give this a try.
def searchTags() {
def tagCriteria = Tag.createCriteria()
def results = tagCriteria.list (max: 50, offset: 10) {
eq("tag", params.selected)
eq("user", lookupPerson())
projections {
property("events")
}
}
[query: params.selected, results: results, resultTotal: results.totalCount]
}
HQL version - The problem is you have to do the same query twice, but select the count(e)
Tag.executeQuery("""
Select e
from Tag t join t.events as e
where t.tag = :selected
and t.user = :user
""", [selected: params.selected, user: lookupPerson()], [max: 50, offset 10])
I am having problems with the next/prev options not displaying on lists with a Groovy on Grails site. I have modified the automatically generated controller code to limit the items in the list to be items that were created by the user. This works fine, however, if the user has more than 10 items, the next/prev buttons don't show up as expected. Below are the relevant code snippits...
Controller:
def list = {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
def login = authenticationService.getSessionUser().getLogin()
def authUser = AuthenticationUser.findByLogin(login)
def userAcct = User.findByLoginID(authUser)
def userServices = Service.createCriteria()
def results
if (userAcct.role == 'admin') {
results = userServices.list(params) {}
} else {
results = userServices.list(params) {
eq("userID", userAcct)
}
}
[serviceInstanceList: results, serviceInstanceTotal: results.count()]
}
GSP:
<div class="paginateButtons">
<g:paginate total="${serviceInstanceTotal}" />
</div>
When I log in with an account with the "admin' role, the next/prev links appear fine. Non-admin accounts do not display the next/prev links when there are more than 10 items to be listed. Can anyone see what I'm doing wrong?
Your criteria should give you a pagedResultList which has a totalCount. So try
to change the last line of your controller to:
[serviceInstanceList: results, serviceInstanceTotal: results.totalCount]