import org.springframework.web.servlet.support.RequestContextUtils as RCU
/**
* This tag enables pagination on the list asynchronously with jquery libraries.
* based on the Facncy jQuery pagination plugin by Cody@
* http://tympanus.net/codrops/2009/11/17/jpaginate-a-fancy-jquery-pagination-plugin/
* @author Alidad Soleimani (alidad@dorisa.com)
*/
class JPaginationTagLib {
// tags:
// total : Total number of records
// update : The id of the element to be updated with reponse
// action : The action to send the request
// controller : The controller to for the request (optional)
// offset : The offset for the records the default is 0
// max : Max number of records to display
// startOffset: starting page (optional)
def remoteJPaginate = {attrs ->
def writer = out
if (attrs.total == null)
throwTagError("Tag [remoteJPaginate] is missing required attribute [total]")
if (attrs.update == null)
throwTagError("Tag [remoteJPaginate] is missing required attribute [update]")
if (!attrs.action)
throwTagError("Tag [remoteJPaginate] is missing required attribute [action]")
def messageSource = grailsAttributes.getApplicationContext().getBean("messageSource")
def locale = RCU.getLocale(request)
def total = attrs.total.toInteger()
def offset = params.offset?.toInteger()
def max = params.max?.toInteger()
def startOffset = params.startOffset?.toInteger()
def linkTagAttrs = attrs
def border = params.border?:false
def display = params.display?.toInteger()
Integer count = (total/max)+1
if (!offset) offset = (attrs.offset ? attrs.offset.toInteger() : 0)
if (!max) max = (attrs.max ? attrs.max.toInteger() : 10)
if (!startOffset) startOffset = (attrs.startOffset ? attrs.startOffset.toInteger() : 1)
if (!display) display = (attrs.display ? attrs.display.toInteger() : 11)
def linkParams = [offset: offset - max, max: max]
def selectParams = [:]
if (params.sort) linkParams.sort = params.sort
if (params.order) linkParams.order = params.order
if (attrs.params) {
linkParams.putAll(attrs.params)
selectParams.putAll(linkParams)
}
writer << "<script type='text/javascript'>"
writer << "\$(function() {"
writer << " \$('#paginatedDiv').paginate({"
writer << " count : ${count},"
writer << " start : ${startOffset},"
writer << " display : ${display},"
writer << " border : ${border},"
writer << " text_color : '#888',"
writer << " background_color : '#EEE',"
writer << " text_hover_color : 'black',"
writer << " background_hover_color : '#CFCFCF',"
writer << " images : false,"
writer << " mouse : 'press',"
writer << " onChange : function(page){"
writer << " var pageOffset = (page-1)* ${max} + eval(${offset}) ;"
writer << " \$.ajax({"
writer << "url: '"
writer << createLink(linkTagAttrs)
writer << "',context: document.body,"
writer << " data: 'page='+ page + '&offset=' + pageOffset + '&max=${max}',"
writer << " cache:false,"
writer << "success: function(response){ \$('#listTemplateDivId').html(response) }"
writer << " });"
writer << "}"
writer << " })"
writer << " });"
writer << "</script>"
writer << " <div id='paginatedDiv'></div>"
}
}