Adding data attribute in form:options, i.e., in options.tag

人走茶凉 提交于 2019-12-24 13:59:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!