Grails g: select optional HTML 5 data attributes

I want to load additional data into each Grails g:select taglib selection option. The required output is as follows:

 <select id="select"> <option value="1" data-foo="dogs">this</option> <option value="2" data-foo="cats">that</option> <option value="3" data-foo="gerbils">other</option> </select> 

I cannot find a way to add additional data to taglib using HTML 5 data attributes. So, how do I achieve a similar result?

+5
source share
3 answers

You can do this with (incorrectly) using closure to display the value (the so-called optionKey in Grails select taglib) of the selection options:

 <g:select from="${books}" optionKey="${{ book -> "${book.id}\" data-author=\"${book.author.name}"}}" optionValue="title" name="selectedBook"/> 

This will give parameters with the data-author attribute:

 <option value="1" data-author="Johann Wolfgang von Goethe">Faust</option> 

This works, at least in Grails 2.4.4 and Grails 3.1.5.

+3
source

As mentioned above, this is currently not possible. To achieve this, you need to write your own tag library.

0
source

You can achieve a similar result using the following

 <select id="select"> <g:each in="${books}" var="book"> <option value="${book.id}" data-foo="${book.slug}">${book.name}</option> </g:each> </select> 
0
source

Source: https://habr.com/ru/post/1246878/


All Articles