source: trunk/grails-app/taglib/CustomTagLib.groovy @ 301

Last change on this file since 301 was 290, checked in by gav, 15 years ago

Secure and work on import asset tree functions.
Improvements to CustomTagLib.

File size: 2.8 KB
Line 
1
2/**
3* General use custom tags.
4* Some are taken from http://www.grails.org/Contribute+a+Tag#checkBoxList
5*/
6class CustomTagLib {
7    static namespace = 'custom'
8
9    def resources = { attrs ->
10        ///@todo: should include our javascript and do setup here.
11    }
12
13    /**
14    * Checkbox list that can be used as a more user-friendly alternative to a multiselect list box.
15     * Usage:
16     * To map the selected ids to corresponding domain objects,
17     * an additional set method is required in the containing domain class:
18     *       //  This additional setter is used to convert the checkBoxList string
19     *       //  of ids selected to the corresponding domain objects.
20     *       public void setAssetSubItemsFromCheckBoxList(ids) {
21     *           def idList = []
22     *           ids.each() {
23     *               if(it.isInteger())
24     *                   idList << it.toInteger()
25     *           }
26     *           this.assetSubItems = idList.collect { AssetSubItem.get( it ) }
27     *       }
28     * Then a line in the controller:
29     *      assetInstance.setAssetSubItemsFromCheckBoxList(params.assetSubItems)
30     */
31    def checkBoxList = {attrs, body ->
32
33        def from = attrs.from
34        def value = attrs.value
35        def cname = attrs.name
36        def isChecked, ht, wd, style, html
37
38        def displayFields = attrs.displayFields
39
40        def displayValue = " "
41
42        // sets the style to override height and/or width if either of them
43        // is specified, else the default from the CSS is taken
44        style = "style='"
45        if(attrs.height)
46            style += "height:${attrs.height};"
47        if(attrs.width)
48            style += "width:${attrs.width};"
49        if(style.length() == "style='".length())
50            style = ""
51        else
52            style += "'" // closing single quote
53
54        html = "<ul class='CheckBoxList' " + style + ">"
55
56        out << html
57
58        from.each { obj ->
59
60            displayValue = " "
61
62            if( displayFields?.contains("id") ) {
63                displayValue += obj.id + " - "
64            }
65
66            if(displayFields?.contains("name")) {
67                displayValue += obj.name
68            }
69            else displayValue += obj
70
71            // if we wanted to select the checkbox using a click anywhere on the label (also hover effect)
72            // but grails does not recognize index suffix in the name as an array:
73            // cname = "${attrs.name}[${idx++}]"
74            // and put this inside the li: <label for='$cname'>...</label>
75
76            isChecked = (value?.contains(obj."${attrs.optionKey}"))? true: false
77
78            out << "<li>" << checkBox(name:cname, value:obj."${attrs.optionKey}", checked: isChecked) << displayValue << "</li>"
79        }
80
81        out << "</ul>"
82
83    } // checkBoxList
84
85} // end class
Note: See TracBrowser for help on using the repository browser.