简体   繁体   中英

How to access enum values from jstl foreach to jQuery

I have a tricky question and need some help. In SomeEnum , I have id, name and icon as parameters.

In html I have a table like this:

<%  pageContext.setAttribute("itemEnum", SomeEnum.values()); %> 
<table id="item_table">    
<c:forEach var="entry" items="${itemEnum}">
    <tr class="item" id="${entry.name}">
        <td><c:out value="${entry.id}"/></td>
    </tr>
</c:forEach>
</table>

In my jquery, I need to get icon and name for each enum object and update row value accordingly.

$("#button").click(function(){
    $('#item_table tr').each(function(item, i) {
    var name = ?? 
    var icon = ??
    });
});

How? Thanks!

From what you've given us it isn't clear where "icon" is coming from. However, I'll offer omething I do on a regular basis and see if it solves your problem.

<%  pageContext.setAttribute("itemEnum", SomeEnum.values()); %> 
<table id="item_table">    
<c:forEach var="entry" items="${itemEnum}">
    <tr class="item" id="${entry.name}" data-name="${entry.name}" data-icon="${entry.icon}">
        <td><c:out value="${entry.id}"/></td>
    </tr>
</c:forEach>
</table>

Then in your jQuery you can do this:

$("#button").click(function(){
    $('#item_table tr').each(function(item, i) {
        var name = $(this).attr("data-name");  // could also be $(this).data("name");
        var icon = $(this).attr("data-icon"); // could also be $(this).data("icon");
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM