How to Get a Index Value from Foreach Loop in Jstl

How to get a index value from foreach loop in jstl

use varStatus to get the index c:forEach varStatus properties

<c:forEach var="categoryName" items="${categoriesList}" varStatus="loop">
<li><a onclick="getCategoryIndex(${loop.index})" href="#">${categoryName}</a></li>
</c:forEach>

JSTL forEach loop optimal way of next index value

Change the inner c:forEach and c:out as shown below that should give your result

<c:forEach var="k" items="${subsplit}" varStatus="status">
<c:if test = "${k == 'de_f'}">
<c:out value="${subsplit[status.index+1]}" /> <!-- This Should print "AE" -->
</c:if>
</c:forEach>

How to get the index of forEach loop in integer

Just store it in a variable, it will actually be an integer:

<c:set var="index" value="${loop.index}" />
<c:set var="index" value="${index + 1}" />
<c:out value="${index}"/>

Should produce 1 2 3 and so on.

How to get index of nested JSTL c:forEach from JSP to JS

In your html you can do like this

<table>

<c:forEach items="${auditBudgetData.auditBudgetTierOneList}" var="auditBudgetTierOne" varStatus="tierOneCount">
** Some Code **
<c:forEach items="${auditBudgetTierOne.auditBudgetTierTwoList}" var="auditBudgetTierTwo" varStatus="tierTwoCount">
** Some Code **
<c:forEach items="${auditBudgetTierTwo.auditBudgetItemList}" var="auditBudgetItem" varStatus="budgetItemCount">

<input type="hidden" name="tierOneIndex" id="tierOneIndex_${budgetItemCount.index}" value="${tierOneCount.count}">
<input type="hidden" name="tierTwoIndex" id="tierTwoIndex_${budgetItemCount.index}" value="${tierTwoCount.count}">
<input type="hidden" name="budgetItemIndex" id ="budgetItemIndex_${budgetItemCount.index}" value="${budgetItemCount.count}">

<tr class="rows" id="${budgetItemCount.index}"><td>click Here</td></tr>

</table>

and in javascript you can do like this

$(document).ready(function(){

$("tr.rows").click(function() {
var rowid=this.id;

var tierOneIndex = $('#tierOneIndex_'+rowid).val();
var tierTwoIndex = $('#tierTwoIndex_'+rowid).val();
var budgetItemIndex = $('#budgetItemIndex_'+rowid).val();

console.log("tierOneIndex:"+tierOneIndex);
console.log("tierTwoIndex:"+tierTwoIndex);
console.log("budgetItemIndex:"+budgetItemIndex);
});

});

Note:

${tierOneCount.index} starts counting at 0

${tierOneCount.count} starts counting at 1

i created one sample fiddle also for you
http://jsfiddle.net/9CHEb/33/

get next element in foreach loop jsp/jstl

Try this:

  ${productList[status.index+1].name}

jstl access a second array value with foreach index

The varstatus variable you are using contains a value "index" that you can use.
But, you can't operate on a string like that (not that I know of at least).
First, you need to convert name2 to a proper array or list. Then you can access it inside the for loop:

${name2list[i.index]}

Now, how to convert it to an array? How about the split function?

<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<c:set var="name2list" value="${fn:split(name2, ',')}"/>

How can I get element from list in JSTL (not forEach)?

You don't need scriptlets to achieve that.

<c:forEach var="account" items="${ctx.model.accounts}" varStatus="loopStatus">
<c:out value="${listName[loopStatus.index]}" />
</c:forEach>

But the fact that you have two parallel lists is, IMHO, a smell. Why don't you have a single list, where each element would allow access to the account and to the corresponding element in listname?



Related Topics



Leave a reply



Submit