Changeset 343


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

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

Location:
trunk/grails-app
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/domain/Asset.groovy

    r329 r343  
    2626    }
    2727
    28     //  This additional setter is used to convert the checkBoxList string
     28    //  This additional setter is used to convert the checkBoxList string or string array
    2929    //  of ids selected to the corresponding domain objects.
    3030    public void setAssetSubItemsFromCheckBoxList(ids) {
    3131        def idList = []
    32         ids.each() {
    33             if(it.isInteger())
    34                 idList << it.toInteger()
     32        if(ids instanceof String) {
     33                if(ids.isInteger())
     34                    idList << ids.toInteger()
     35        }
     36        else {
     37            ids.each() {
     38                if(it.isInteger())
     39                    idList << it.toInteger()
     40            }
    3541        }
    3642        this.assetSubItems = idList.collect { AssetSubItem.get( it ) }
  • trunk/grails-app/domain/Person.groovy

    r294 r343  
    5555    String toString() {"${this.firstName} ${this.lastName}"}
    5656
    57     //  This additional setter is used to convert the checkBoxList string
     57    //  This additional setter is used to convert the checkBoxList string or string array
    5858    //  of ids selected to the corresponding domain objects.
    5959    public void setPersonGroupsFromCheckBoxList(ids) {
    6060        def idList = []
    61         ids.each() {
    62             if(it.isInteger())
    63                 idList << it.toInteger()
     61        if(ids instanceof String) {
     62                if(ids.isInteger())
     63                    idList << ids.toInteger()
     64        }
     65        else {
     66            ids.each() {
     67                if(it.isInteger())
     68                    idList << it.toInteger()
     69            }
    6470        }
    6571        this.personGroups = idList.collect { PersonGroup.get( it ) }
  • 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.