| 1 | |
|---|
| 2 | /** |
|---|
| 3 | * General use custom tags. |
|---|
| 4 | * Some are taken from http://www.grails.org/Contribute+a+Tag#checkBoxList |
|---|
| 5 | */ |
|---|
| 6 | class 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 | // sets the style to override height and/or width if either of them |
|---|
| 39 | // is specified, else the default from the CSS is taken |
|---|
| 40 | style = "style='" |
|---|
| 41 | if(attrs.height) |
|---|
| 42 | style += "height:${attrs.height};" |
|---|
| 43 | if(attrs.width) |
|---|
| 44 | style += "width:${attrs.width};" |
|---|
| 45 | if(style.length() == "style='".length()) |
|---|
| 46 | style = "" |
|---|
| 47 | else |
|---|
| 48 | style += "'" // closing single quote |
|---|
| 49 | |
|---|
| 50 | html = "<ul class='CheckBoxList' " + style + ">" |
|---|
| 51 | |
|---|
| 52 | out << html |
|---|
| 53 | |
|---|
| 54 | from.each { obj -> |
|---|
| 55 | |
|---|
| 56 | // if we wanted to select the checkbox using a click anywhere on the label (also hover effect) |
|---|
| 57 | // but grails does not recognize index suffix in the name as an array: |
|---|
| 58 | // cname = "${attrs.name}[${idx++}]" |
|---|
| 59 | // and put this inside the li: <label for='$cname'>...</label> |
|---|
| 60 | |
|---|
| 61 | isChecked = (value?.contains(obj."${attrs.optionKey}"))? true: false |
|---|
| 62 | |
|---|
| 63 | out << "<li>" << checkBox(name:cname, value:obj."${attrs.optionKey}", checked: isChecked) << " ${obj.id} - ${obj.name}" << "</li>" |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | out << "</ul>" |
|---|
| 67 | |
|---|
| 68 | } // checkBoxList |
|---|
| 69 | |
|---|
| 70 | } // end class |
|---|