问题
I have a list of objects in the pageContext and I want some of it's attribute in the data attribute of the option tag of select.
Below is the jsp code :
<form:select class="required chzn_a" disabled="${view}" path="one.id" id="one">
<form:option value="">Select</form:option>
<form:options items="${objectList}" itemValue="id" itemLabel="name"/>
</form:select>
I want something like below :
However, below is what I am getting :
回答1:
As far as I know the form:options tag does not support customization like this. Instead you can loop through the options yourself like this:
<form:select class="required chzn_a" disabled="${view}" path="one.id" id="one">
<form:option value="">Select</form:option>
<c:forEach items="${objectList}" var="objectListEntry">
<form:option value="${objectListEntry.id}" data-code="${objectListEntry.code}">${objectListEntry.name}</form:option>
</c:forEach>
</form:select>
The spring form tags implements the DynamicAttributes interface and all attributes not explicitly defined in the tld are added to the generated tag.
来源:https://stackoverflow.com/questions/33543997/adding-data-attribute-in-formoptions-i-e-in-options-tag