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
Advantage of Set and Get Methods VS Public Variable
Java Keytool How to Add Server Cert from Url/Port
Annotation to Filter Results of a @Onetomany Association
Binding a List in @Requestparam
Getting Active Window Information in Java
How to Cast List<Object> to List<Myclass>
Java Stanford Nlp: Part of Speech Labels
Can't Make Jackson and Lombok Work Together
How to Retrieve the Autoincrement Id from a Prepared Statement
Jni Converting Jstring to Char *
How to Make Cartesian Product with Java 8 Streams
How to Use the Command Update-Alternatives --Config Java