Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import org.springframework.web.servlet.support.RequestContextUtils as RCU
  2.  
  3. /**
  4.  * This tag enables pagination on the list asynchronously with jquery libraries.
  5.  * based on the Facncy jQuery pagination plugin by Cody@
  6.  * http://tympanus.net/codrops/2009/11/17/jpaginate-a-fancy-jquery-pagination-plugin/
  7.  * @author Alidad Soleimani (alidad@dorisa.com)
  8.  */
  9. class JPaginationTagLib {
  10.     // tags:
  11.     //     total       : Total number of records
  12.     //     update       : The id of the element to be updated with reponse
  13.     //     action      : The action to send the request
  14.     //     controller : The controller to for the request (optional)
  15.     //       offset        : The offset for the records the default is 0
  16.     //     max        : Max number of records to display
  17.     //     startOffset: starting page (optional)
  18.    
  19.     def remoteJPaginate = {attrs ->
  20.         def writer = out
  21.  
  22.         if (attrs.total == null)
  23.             throwTagError("Tag [remoteJPaginate] is missing required attribute [total]")
  24.  
  25.         if (attrs.update == null)
  26.             throwTagError("Tag [remoteJPaginate] is missing required attribute [update]")
  27.  
  28.         if (!attrs.action)
  29.             throwTagError("Tag [remoteJPaginate] is missing required attribute [action]")
  30.  
  31.         def messageSource = grailsAttributes.getApplicationContext().getBean("messageSource")
  32.         def locale = RCU.getLocale(request)
  33.  
  34.         def total = attrs.total.toInteger()
  35.         def offset = params.offset?.toInteger()
  36.         def max = params.max?.toInteger()
  37.         def startOffset =  params.startOffset?.toInteger()
  38.         def linkTagAttrs = attrs
  39.         def border = params.border?:false
  40.         def display  = params.display?.toInteger()
  41.        
  42.         Integer count = (total/max)+1
  43.         if (!offset) offset = (attrs.offset ? attrs.offset.toInteger() : 0)
  44.         if (!max) max = (attrs.max ? attrs.max.toInteger() : 10)
  45.         if (!startOffset) startOffset = (attrs.startOffset ? attrs.startOffset.toInteger() : 1)
  46.         if (!display) display = (attrs.display ? attrs.display.toInteger() : 11)
  47.         def linkParams = [offset: offset - max, max: max]
  48.         def selectParams = [:]
  49.         if (params.sort) linkParams.sort = params.sort
  50.         if (params.order) linkParams.order = params.order
  51.  
  52.         if (attrs.params) {
  53.             linkParams.putAll(attrs.params)
  54.             selectParams.putAll(linkParams)
  55.         }
  56.  
  57.  
  58.         writer << "<script type='text/javascript'>"
  59.         writer << "\$(function() {"
  60.         writer << "    \$('#paginatedDiv').paginate({"
  61.         writer << "        count         : ${count},"
  62.         writer << "        start         : ${startOffset},"
  63.         writer << "        display     : ${display},"
  64.         writer << "        border                    : ${border},"
  65.         writer << "        text_color              : '#888',"
  66.         writer << "        background_color        : '#EEE',"
  67.         writer << "        text_hover_color          : 'black',"
  68.         writer << "        background_hover_color    : '#CFCFCF',"
  69.         writer << "        images        : false,"
  70.         writer << "        mouse        : 'press',"
  71.         writer << "        onChange    :  function(page){"
  72.         writer << "     var pageOffset = (page-1)* ${max} + eval(${offset}) ;"
  73.         writer << " \$.ajax({"
  74.         writer << "url: '"
  75.         writer << createLink(linkTagAttrs)
  76.         writer << "',context: document.body,"
  77.         writer << " data: 'page='+ page + '&offset=' + pageOffset + '&max=${max}',"
  78.         writer << " cache:false,"
  79.         writer << "success: function(response){ \$('#listTemplateDivId').html(response) }"
  80.         writer <<  " });"
  81.         writer << "}"
  82.         writer << "    })"
  83.         writer << "    });"
  84.         writer << "</script>"
  85.         writer << "    <div id='paginatedDiv'></div>"
  86.                
  87.     }
  88. }