Add filter pane to your scaffolding list template

The filter pane plugin is one of my favorite plugins, to give the user the capability to filter lists.

But it’s a boring task, to add the filter pane to each scaffolded view. Especially you’ve to redo all this work, after regenerating the list view. But Customizing the Scaffolding templates will get you rid of this. All you have to do is:

  • Install the filter pane plugin, if not already done:
    grails install-plugin filterpane
  • Copy the the templates used by Grails during code generation to your project directory, if not already done:
    grails install-templates
  • Customize the controller template src/templates/scaffolding/Controller.groovy:
    class ${className}Controller {
       def filterPaneService
       ...
       def filter = {
          if(!params.max) params.max = 10
          render( view:'list',
                  model:[ ${propertyName}List: filterPaneService.filter( params, ${className} ),
                          ${propertyName}Total: filterPaneService.count( params, ${className} ),
                 filterParams: org.grails.plugin.filterpane.FilterPaneUtils.extractFilterParams(params),
                 params:params ] )
       }
  • Customize the list template viewsrc/templates/scaffolding/list.gsp:
    <head>
        ...
        <filterpane:includes />
    </head>
    <body>
        ...
        <div class="pagination">
           <g:paginate total="\${${propertyName}Total}" params="\${filterParams}"/>
           <filterpane:filterButton />
        </div>
        </div>
        <filterpane:filterPane domain="${domainClass.fullName}"    />
    </body>
    

That’s all!

Ein Gedanke zu „Add filter pane to your scaffolding list template“

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert