题
我试图过程中使用JSTL的列表。我想比其他人不同的方式对待列表的第一个元素。即,我想仅第一元件具有显示组到块外,其余应该被隐藏。
我有什么似乎有些力不从心,而无法正常工作。
感谢您的帮助。
<c:forEach items="${learningEntry.samples}" var="sample">
<!-- only the first element in the set is visible: -->
<c:if test="${learningEntry.samples[0] == sample}">
<table class="sampleEntry">
</c:if>
<c:if test="${learningEntry.samples[0] != sample}">
<table class="sampleEntry" style="display:hidden">
</c:if>
解决方案
这是可以做到更短,而不<c:if>
:
<c:forEach items="${learningEntry.samples}" var="sample" varStatus = "status">
<table class="sampleEntry" ${status.first ? '' : 'style = "display:none"'}>
</c:forEach>
其他提示
是,申报varStatus =“STAT”在foreach元素,所以你可以要求它,如果它是第一个还是最后一个。其类型LoopTagStatus的变量。
这是LoopTagStatus该文档: HTTP: //java.sun.com/products/jsp/jstl/1.1/docs/api/javax/servlet/jsp/jstl/core/LoopTagStatus.html 它有更多有趣的属性...
<c:forEach items="${learningEntry.samples}" var="sample" varStatus="stat">
<!-- only the first element in the set is visible: -->
<c:if test="${stat.first}">
<table class="sampleEntry">
</c:if>
<c:if test="${!stat.first}">
<table class="sampleEntry" style="display:none">
</c:if>
<强>编辑:从axtavt复制强>
这是可以做到更短,而不<c:if>
:
<c:forEach items="${learningEntry.samples}" var="sample" varStatus = "status">
<table class="sampleEntry" ${status.first ? '' : 'style = "display:none"'}>
</c:forEach>
不隶属于 StackOverflow