Jak uzyskać wartość indeksu z pętli foreach w jstl

Mam ustawioną wartość w obiekcie request Jak poniżej,

String[] categoriesList=null;
categoriesList = engine.getCategoryNamesArray();
request.setAttribute("categoriesList", categoriesList );

I tak to robię na stronie jsp

<% if(request.getAttribute("categoriesList") != null) { %>
<c:forEach var="categoryName" items="${categoriesList}">
   <li><a onclick="getCategoryIndex()" href="#">${categoryName}</a></li>
</c:forEach>
<% }%>

Jak uzyskać indeks każdego elementu i przekazać go do funkcji JavaScript onclick="getCategoryIndex()".

Author: David Newcomb, 2013-09-16

4 answers

Użyj varStatus aby uzyskać indeks c: forEach varstatus properties

<c:forEach var="categoryName" items="${categoriesList}" varStatus="loop">
    <li><a onclick="getCategoryIndex(${loop.index})" href="#">${categoryName}</a></li>
</c:forEach>
 183
Author: newuser,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-04 20:55:37

Możesz użyć atrybutu varStatus w następujący sposób: -

<c:forEach var="categoryName" items="${categoriesList}" varStatus="myIndex">

MyIndex.indeks da ci indeks. Tutaj myIndex znajduje się obiektLoopTagStatus .

Stąd możesz wysłać to do swojej metody javascript w następujący sposób: -

<a onclick="getCategoryIndex(${myIndex.index})" href="#">${categoryName}</a>
 11
Author: SudoRahul,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-17 09:48:35

Mam podobny problem teraz rozumiem, że mamy więcej opcji : varStatus="loop", tutaj będzie zmienna loop will, która będzie trzymać indeks lop.

Może służyć do odczytu indeksu bazowego Zeora lub 1 indeksu bazowego.

${loop.count}` it will give 1 starting base index.

${loop.index} it will give 0 base index as normal Index of array zacznij od 0.

Na Przykład:

<c:forEach var="currentImage" items="${cityBannerImages}" varStatus="loop">
<picture>
   <source srcset="${currentImage}" media="(min-width: 1000px)"></source>
   <source srcset="${cityMobileImages[loop.count]}" media="(min-width:600px)"></source>
   <img srcset="${cityMobileImages[loop.count]}" alt=""></img>
</picture>
</c:forEach>

Aby uzyskać więcej informacji, zapoznaj się z tym link

 11
Author: Laxman G,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-01 09:38:08

${categoryName}

Above line was giving error for me. więc zapisałem poniżej sposób, który działa dobrze dla mnie. ') "href="# " >${categoryName}

Może ktoś inny może dostać ten sam błąd. Spójrz na nich!!

 0
Author: Rakesh Kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-22 08:13:17