Tuesday, July 20, 2010

making export plugin work with filterpane

The export plugin and the filterpane plugin for themselves work like a charm. If you try to export your filtered list however, you run into problems. The root of this problem has been recognized during an earlier discussion on the mailing list.

The problem: You have to extend the filter action to deal with the export. Therefore you have to apply the filterService.filter method with the same filter params as when you applied the actual filter.

The workaround (not a perfect solution): To solve this problem I save the filter params in the session. When export is applied on the filter action the filter params are retrieved from the session. That works well although I'm not very happy with storing so much information in the session permanently.

The Code:

Here I have modified the example from the plugin page. The export-part of the list action is extracted and gets its own export closure, which can then be used by the filter action as well. I also do not explicitly name the parameters as I am using this code in a scaffolded controller. Therefore a list is fetched of all properties, excluded those that are not really representative.

def export = {attrs ->
  
  def response = attrs.response
  println attrs.exportList
  
  def excluded = grails.persistence.Event.allEvents.toList() + ["mapping", "lastModifierId", "hasMany", "class", "belongsTo", "constraints", "searchable", "attached", "errors", "metaClass", "log", "object", "version", "beforeInsert", "beforeUpdate", "mappedBy", "springSecurityService", "type", "typeLabel"]
  List fields = ${className}.newInstance().properties.keySet().toList().findAll { !excluded.contains(it) && !isId(it)}

  response.contentType = org.codehaus.groovy.grails.commons.ConfigurationHolder.config.grails.mime.types[attrs.format]
  response.setHeader("Content-disposition", "attachment; filename=${className}.\${attrs.extension}")
  
  exportService.export(attrs.format, response.outputStream, attrs.exportList, fields, [:], [:], [:])
 }
 
 def isId(def label)
 {
  label.reverse().startsWith("dI")
 }

 def filter = {  
  if(!params.max) params.max = 10
  println "out:" +params
  if(params?.format && params.format != "html" && session.filterParams)
  {
   def exportList = filterService.filter( session.filterParams, ${className} )
   println "list:" + exportList
   export(response: response, extension: params.extension, format: params.format, exportList: exportList)
  }
  
  session.filterParams = params
  
  render( view:'list',
   model:[ ${propertyName}List: filterService.filter( params, ${className} ),
   ${propertyName}Total: filterService.count( params, ${className} ),
   filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params),
   params:params ] )
 }

EDIT: Steve made me aware that it is easy to extract the necessary parameters with a helper method. One should therefore rather modify the export taglib to pass the correct parameters to the filter action. Also see Steve's comment below.

EDIT: Please forget about most of the crazy stuff I have done here :-) You can pass the filterParams with the export tag like this:


3 comments:

  1. The FilterUtils class that comes with the filterpane plugin contains a static method "extractFilterParams" that will return you a submap of all the properties related to the filterpane. I created it for adding filterpane related params to various tags like sortedColumn without adding the entire params map.

    ReplyDelete
  2. I used your way and it works well for me. Thanks for sharing your thoughts

    ReplyDelete
  3. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here.
    Kindly keep blogging. If anyone wants to become a Front end developer learn from HTML5 CSS3 Javascript Online Training from India .
    or learn thru HTML5 CSS3 Javascript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry.
    HTML5 CSS3 Javascript Online Training from India

    ReplyDelete