I have a query which gives a result of about 100 rows. Here is the code for the controllers
def bandsOneTrack = {
def bands = Band.executeQuery("Select b from Band as b where size(b.tracks) > (select count(t.id) from Band as ba join ba.tracks as t where ba.id = b.id and t.ourContract is not null) and (select count(t.id) from Band as ba join ba.tracks as t where ba.id = b.id and t.ourContract is not null) >= 1" )
render(view: 'bands_list' , model: [ bands : bands ])
}
It gives me the result set of about 100 rows but they are appearing inside a same page.
Now I want to use pagination so that I can restrict it to only 20 rows per page.
What should I do, and also how to use pagination for this.
On your pagination tag check the total parameter. That should be the total number of records. In your case 100 so that the pagination tag can calculate the total number of pages.
Something like this here:
<g:paginate controller="Book" action="list" total="${bookInstanceTotal}" />
You might need to execute your query once to find the total number of records.
def list() {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
def ls = Book.executeQuery("from Book a",[max: params.max, offset: params.offset])
def totalCount = Book.executeQuery("from Book a").size()
[bookInstanceList: ls, bookInstanceTotal: totalCount]
}
If I remember correctly, you add max and offset properties to the model you're passing to your view, it should automatically wire in pagination. At that point, you should be able to use the paginate tag to iterate through your result sets. See the docs here:
http://grails.org/doc/latest/ref/Tags/paginate.html
Grails Criteria Query and pagination params
params.max = params?.max as Integer ?: 10
params.offset = params?.offset as Integer ?: 0
params.sort = params?.sort ?: "email"
params.order = params?.order ?: "asc"
params.filter = params?.filter ?: ""
params.packet = params?.packet ?: ""
def members = Member.createCriteria().list(params)
{
or
{
if(params.filter != ""){
ilike("firstName", "%" + params.filter + "%")
ilike("lastName", "%" + params.filter + "%")
ilike("email", "%" + params.filter + "%")
try {
params.filter as Long
eq("citizenId" , params.filter.toLong())
}catch (e) {
}
ilike("mobile", "%" + params.filter + "%")
}
}
}
def dataMembers = [:]
dataMembers.data = members
dataMembers.totalRecord = members.totalCount
render dataMembers as JSON
Output
{
"data": [
{
"id":1,
"firstName":name
},
{
"id":2,
"firstName":name
}
],
"totalRecord":5
}
Related
I have a domain class Project as below
class Project {
String projectName
String projectCode
String techLead
String projectManager
Date deliveryDate
String currentPhase
Integer priority
}
I have controller as below
class ProjectController {
def scaffold = Project
def index = {
redirect(action:list,params:params)
}
def list = {
// displays only 10 records per page
if (!params.max) params.max = 10
[ projectList: Project.list( params ) ]
}
}
I would like to display the list of projects in the sorting order or priority. How can I implement that ?
change your list action to the below
def list = {
// displays only 10 records per page
if (!params.max) {
params.max = 10
}
params.sort = "priority"
params.order = "asc" // change to "desc" to sort in the opposite direction
[projectList: Project.list(params)]
}
A much shorter and more idiomatic way of doing this would be to use the dynamic methods on list that provide ordering:
def list = {
[projectList: Project.listOrderByPriority(max: params.max ?: 10)]
}
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])
How to specify conditions in each function in Rails.
#page = 1
#allRecords = #This result will come from API.. It have 500 records.
resultArray = []
#allRecords.each do |t| #need to specify condition here based on #page value
#processes...
resultArray << t
end
render :json => resultArray
i will send #page number to controller. Then resultArray will sent final values
Input : #page = 1
Output : resultArray = Records 1 - 50
Input : #page = 2
Output : resultArray = Records 51 - 100
Input : #page = 3
Output : resultArray = Records 101 - 150
like wise...
How can i achieve this.
Any help would be appreciated. Thanks.
#page = 1
#allRecords = #This result will come from API.. It have 500 records.
per_page = 50
resultArray = #allRecords.slice(#page + (#page - 1) * per_page, per_page)
# if result is not Array (Enumerable)
#resultArray = #allRecords.to_a.slice(#page + (#page - 1) * per_page, per_page)
I am new to Grails criteria builder, can someone please explain what does the following mean?
def c = Account.createCriteria()
def results = c {
like("holderFirstName", "Fred%")
and {
between("balance", 500, 1000)
eq("branch", "London")
}
maxResults(10)
order("holderLastName", "desc")
}
Does it mean
Select * from account where
holderFirstName like 'fred%' and
(balance between 500 and 1000 **and**
branch='london')
Select * from account where
holderFirstName like 'fred%' and
(balance between 500 and 1000 **or**
branch='london')
If i want to use "or" and "and" together how do I do that?
Your example would execute as:
select * from account
where holderFirstName like 'Fred%'
and balance between 500 and 1000
and branch = 'London'
All top level conditions are implied to be AND'ed together. You could create the same Criteria as:
def c = Account.createCriteria()
def results = c {
like("holderFirstName", "Fred%")
between("balance", 500, 1000)
eq("branch", "London")
maxResults(10)
order("holderLastName", "desc")
}
To get your second query use this Criteria:
def c = Account.createCriteria()
def results = c {
like("holderFirstName", "Fred%")
or {
between("balance", 500, 1000)
eq("branch", London")
}
maxResults(10)
order("holderLastName", "desc")
}
Nest your and / or closures for more complex Criteria.
Your current criteria means "and" in the two conditions of balance and branch. so
Select * from account where holderFirstName like 'fred%' and (balance between 500 and 1000 and branch='london') is correct, just that it will hold maximum 10 results and will be sorted on the basis of "holderLastName" in descending order.
To use and and or together you also need to specify an or block in criteria, so your criteria will look something like
Account.createCriteria()
def results = c {
like("holderFirstName", "Fred%")
and {
between("balance", 500, 1000)
eq("branch", "London")
}
or{
eq("prop1", prop1)
eq("prop2",prop2)
}
maxResults(10)
order("holderLastName", "desc")
}
in this criteria there is an and between balance and branch conditions. and also an or between prop1 and prop2
I've been using tags in my projects. I was browsing the custom tags on grails.org to find some new tags for my library.
http://www.grails.org/Contribute+a+Tag
I was wondering if people in the StackOverflow community have a favorite custom tag that they would like to share.
I find the DecimalFormat class (and Grails's formatNumber tag by extension) a bit opaque for certain use cases, and I still haven't found a reasonable way to do some pretty basic formatting with it without some ugly pre-processing to generate an appropriate format string. I threw together a simple number formatting tag several months ago which essentially constructs a format string and does some minimal processing to the number itself.
It's not as generic or elegant as I'd like (it's all we needed at the time - it's super basic, but it still keeps some ugly processing out of GSPs), but it should be easy to read, and it's obvious where it could be trivially improved (i.e. making the scaling iterative instead of naive if-elseif slop, allowing the user to pass in custom scaling markers, allowing for a custom number validator as a parameter, etc.).
// Formats a number to 3 significant digits, appending appropriate scale marker
// (k, m, b, t, etc.). Defining var allows you to use a string representation
// of the formatted number anywhere you need it within the tag body, and
// provides the scale as well (in case highlighting or other special formatting
// based upon scale is desired).
def formatNumberScaled = {attrs, body -> // number, prefix, suffix, invalid, var
Double number
String numberString
String scale
try {
number = attrs.'number'.toDouble()
} catch (Exception e) {
number = Double.NaN
}
if (number.isNaN() || number.isInfinite()) {
numberString = scale = attrs.'invalid' ?: "N/A"
} else {
Boolean negative = number < 0d
number = negative ? -number : number
if (number < 1000d) {
scale = ''
} else if (number < 1000000d) {
scale = 'k'
number /= 1000d
} else if (number < 1000000000d) {
scale = 'm'
number /= 1000000d
} else if (number < 1000000000000d) {
scale = 'b'
number /= 1000000000d
} else if (number < 1000000000000000d) {
scale = 't'
number /= 1000000000000d
}
String format
if (number < 10d) {
format = '#.00'
} else if (number < 100d) {
format = '##.0'
} else {
format = '###'
}
format = "'${attrs.'prefix' ?: ''}'${format}'${scale} ${attrs.'suffix' ?: ''}'"
numberString = g.formatNumber('number': negative ? -number : number, 'format': format)
}
// Now, either print the number or output the tag body with
// the appropriate variables set
if (attrs.'var') {
out << body((attrs.'var'): numberString, 'scale': scale)
} else {
out << numberString
}
}
I have a "fmt:relDate" tag that gives you Twitter-like relative dates "3 days ago", "less than 30 seconds ago", etc., with the real time as a tooltip.
The current implementation is basically a gigantic chain of if/then statements with the boundaries that I like. A binary-search based algorithm would be better (in the sense of "more efficient"), and the current implementation has my personal preferences encoded into it, so I'm reluctant to share the tag.
I have a remote paginate tab, that helps me paginate results via ajax. Its an improvement over the default tab and takes in custom arguments.
Here's the code:
class CustomRemotePaginateTagLib {
static namespace = 'myTagLib'
/** * Creates next/previous links to support pagination for the current controller * * <g:paginate total="$ { Account.count() } " /> */
def remotePaginate = {attrs ->
def writer = out
if (attrs.total == null) throwTagError("Tag [remotePaginate] is missing required attribute [total]")
if (attrs.update == null) throwTagError("Tag [remotePaginate] is missing required attribute [update]")
def locale = RequestContextUtils.getLocale(request)
def total = attrs.total.toInteger()
def update = attrs.update
def action = (attrs.action ? attrs.action : (params.action ? params.action : "list"))
def controller = (attrs.controller ? attrs.controller : params.controller)
def offset = params.offset?.toInteger()
def max = params.max?.toInteger()
def maxsteps = (attrs.maxsteps ? attrs.maxsteps.toInteger() : 10)
if (!offset) offset = (attrs.offset ? attrs.offset.toInteger() : 0)
if (!max) max = (attrs.max ? attrs.max.toInteger() : 10)
def linkParams = [offset: offset - max, max: max]
if (params.sort) linkParams.sort = params.sort
if (params.order) linkParams.order = params.order
if (attrs.params) linkParams.putAll(attrs.params)
linkParams['action'] = action
linkParams['controller'] = controller
def linkTagAttrs = [url: "#"]
if (attrs.controller) { linkTagAttrs.controller = attrs.controller }
if (attrs.id != null) { linkTagAttrs.id = attrs.id }
// determine paging variables
def steps = maxsteps > 0
int currentstep = (offset / max) + 1
int firststep = 1
int laststep = Math.round(Math.ceil(total / max))
// display previous link when not on firststep
if (currentstep > firststep) {
linkTagAttrs.class = 'prevLink'
def prevOffset = linkParams.offset
def params = attrs.params ?: []
params.'offset' = prevOffset
linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
writer << link(linkTagAttrs.clone()) {
(attrs.prev ? attrs.prev : g.message(code: 'default.paginate.prev', default: 'Previous'))
}
}
// display steps when steps are enabled and laststep is not firststep
if (steps && laststep > firststep) {
linkTagAttrs.class = 'step'
// determine begin and endstep paging variables
int beginstep = currentstep - Math.round(maxsteps / 2) + (maxsteps % 2)
int endstep = currentstep + Math.round(maxsteps / 2) - 1
if (beginstep < firststep) {
beginstep = firststep
endstep = maxsteps
}
if (endstep > laststep) {
beginstep = laststep - maxsteps + 1
if (beginstep < firststep) {
beginstep = firststep
}
endstep = laststep
}
// display firststep link when beginstep is not firststep
if (beginstep > firststep) {
linkParams.offset = 0
def params = attrs.params ?: []
params['offset'] = linkParams.offset
linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
writer << link(linkTagAttrs.clone()) { firststep.toString() }
writer << '<span class="step">..</span>'
}
// display paginate steps
(beginstep..endstep).each {i ->
if (currentstep == i) {
writer << "<span class=\"currentStep\">${i}</span>"
} else {
linkParams.offset = (i - 1) * max
def params = attrs.params ?: []
params['offset'] = linkParams.offset
linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
writer << link(linkTagAttrs.clone()) { i.toString() }
}
}
// display laststep link when endstep is not laststep
if (endstep < laststep) {
writer << '<span class="step">..</span>'
linkParams.offset = (laststep - 1) * max
def params = attrs.params ?: []
params['offset'] = linkParams.offset
linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
writer << link(linkTagAttrs.clone()) { laststep.toString() }
}
}
// display next link when not on laststep
if (currentstep < laststep) {
linkTagAttrs.class = 'nextLink'
linkParams.offset = offset + max
def params = attrs.params ?: []
params['offset'] = linkParams.offset
linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
writer << link(linkTagAttrs.clone()) {
(attrs.next ? attrs.next : g.message(code: 'default.paginate.next', default: 'Next'))
}
}
}