{"id":1430,"date":"2010-08-10T21:49:11","date_gmt":"2010-08-10T16:19:11","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1430"},"modified":"2010-08-10T21:50:29","modified_gmt":"2010-08-10T16:20:29","slug":"creating-select-tag-with-optgroup-in-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/creating-select-tag-with-optgroup-in-grails\/","title":{"rendered":"Creating select tag with optgroup in Grails"},"content":{"rendered":"<p>Hi,<br \/>\nIn one of my projects i needed to create a select-input tag with OPTGROUP options.<br \/>\nI searched a lot about it and  found that  g:select  tag do not support OPTGROUP<\/p>\n<p>So to solve this problem, i created a simple optgroup tag using Html and grails taglib.<\/p>\n<p>I created a Map for the values to be used in the select tag and pass it to the test.gsp page from the controller.<br \/>\nSay  Map dataMap=[&#8220;Car&#8221;:[&#8220;Car A&#8221;,&#8221;Car B&#8221;,&#8221;Car C&#8221;],&#8221;Truck&#8221;:[&#8220;Truck A&#8221;,&#8221;Truck B&#8221;]]<\/p>\n<p>I put the tag in test.gsp where i want to display the select dropdown<\/p>\n<pre lang=\"groovy\">\r\n\r\n         <test:optGroup name = \"data\" dataMap=\"${dataMap}\" \/>\r\n\r\n<\/pre>\n<p>In my TestTaglib.groovy, i defined the tag as<\/p>\n<pre lang=\"groovy\">\r\n\r\nclass TestTagLib {\r\n    static namespace = 'test'\r\n\r\n    def optGroup = {attrs ->\r\n        Map dataMap = attrs['dataMap']\r\n        out << g.render(template: 'layouts\/optSelect', model: [dataMap:dataMap])\r\n    }\r\n}\r\n\r\n<\/pre>\n<p>I created a template \"_optSelect.gsp\" under the layouts folder<\/p>\n<pre lang=\"groovy\">\r\n\r\n<select name=\"mainList\">\r\n    <optgroup label=\"--\"><option value=\"\">(Select One)<\/option><\/optgroup>\r\n    <g:each in=\"${dataMap}\" var=\"data\">\r\n        <optgroup label=\"${data.key}\">\r\n            <g:each in=\"${data.value}\" var=\"value\">\r\n                <option value=\"${value}\">${value}<\/option>\r\n            <\/g:each>\r\n        <\/optgroup>\r\n    <\/g:each>\r\n<\/select>\r\n\r\n\r\n<\/pre>\n<p>This generated the required SELECT dropdown with OPTGROUP and solved the problem.<\/p>\n<p>Hope it helps.<\/p>\n<p>Regards:-<\/p>\n<p>Vishal Sahu<br \/>\nvishal@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, In one of my projects i needed to create a select-input tag with OPTGROUP options. I searched a lot about it and found that g:select tag do not support OPTGROUP So to solve this problem, i created a simple optgroup tag using Html and grails taglib. I created a Map for the values to [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1430"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1430"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1430\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}