Ignore:
Timestamp:
Feb 10, 2010, 2:49:13 AM (14 years ago)
Author:
gav
Message:

Improvements to CustomTagLib checkBoxList, sorting, dynamic displayFields and a small fix to the domain class set method.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/taglib/CustomTagLib.groovy

    r303 r343  
    1616     * To map the selected ids to corresponding domain objects,
    1717     * an additional set method is required in the containing domain class:
    18      *       //  This additional setter is used to convert the checkBoxList string
     18     *       //  This additional setter is used to convert the checkBoxList string or string array
    1919     *       //  of ids selected to the corresponding domain objects.
    2020     *       public void setAssetSubItemsFromCheckBoxList(ids) {
    2121     *           def idList = []
    22      *           ids.each() {
    23      *               if(it.isInteger())
    24      *                   idList << it.toInteger()
     22     *           if(ids instanceof String) {
     23     *                   if(ids.isInteger())
     24     *                       idList << ids.toInteger()
     25     *           }
     26     *           else {
     27     *               ids.each() {
     28     *                   if(it.isInteger())
     29     *                       idList << it.toInteger()
     30     *               }
    2531     *           }
    2632     *           this.assetSubItems = idList.collect { AssetSubItem.get( it ) }
     
    3541     *    value - the current value.
    3642     *    optionKey - the key to use.
    37      *    displayFields - (optional) available options are 'id' and 'name', defaults to the objects toString().
     43     *    sortBy - (optional) the attribute to sort the from list by.
     44     *    displayFields - (optional) defaults to the objects toString()
     45     *    displayFieldsSeparator - (optional) defaults to a space.
    3846     *    linkController - (optional, requires linkAction.) the controller to use for a link to the objects in the checkBoxList.
    3947     *    linkAction - (optional, requires linkController.) the action to use for a link to the objects in the checkBoxList.
     
    4553     *                                    value="${assetInstance?.assetSubItems.collect{it.id}}"
    4654     *                                    optionKey="id"
     55     *                                    sortBy="description"
    4756     *                                    displayFields="['id', 'name']"
     57     *                                    displayFieldsSeparator=', '
    4858     *                                    linkController="assetSubItemDetailed"
    4959     *                                    linkAction="show"/>
     
    5969        def isChecked, ht, wd, style, html
    6070
     71        def sortBy = attrs.sortBy
    6172        def displayFields = attrs.displayFields
     73        def displayFieldsSeparator = attrs.displayFieldsSeparator ?: ' '
    6274        def linkController = attrs.linkController
    6375        def linkAction = attrs.linkAction
     
    8193        out << html
    8294
     95        if(sortBy)
     96            from.sort { p1, p2 -> p1[sortBy].compareToIgnoreCase(p2[sortBy]) }
     97
    8398        from.each { obj ->
    8499
    85100            displayValue = " "
    86101
    87             if( displayFields?.contains("id") )
    88                 displayValue += obj.id + " - "
    89 
    90102            if(linkController && linkAction)
    91103                   displayValue += "<a href=\"${createLink(controller: linkController, action: linkAction, id: obj.id).encodeAsHTML()}\">"
    92104
    93             if(displayFields?.contains("name")) {
    94                 displayValue += obj.name
     105            if(displayFields) {
     106                displayValue += displayFields.collect { obj[it] }.join(displayFieldsSeparator)
    95107            }
    96             else displayValue += obj
     108            else displayValue += obj // use the obj's default toString()
    97109
    98110            if(linkController && linkAction)
    99                 displayValue += '</a>'
     111                displayValue += "</a>"
    100112
    101113            // if we wanted to select the checkbox using a click anywhere on the label (also hover effect)
Note: See TracChangeset for help on using the changeset viewer.