Frage

Ich versuche, eine Liste mit jstl zu verarbeiten. Ich möchte das erste Element der Liste anders als der Rest behandeln. Und zwar möchte ich nur das erste Element Display eingestellt haben zu blockieren, sollte der Rest versteckt werden.

Was ich habe, scheint aufgebläht, und funktioniert nicht.

Vielen Dank für jede Hilfe.

<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>
War es hilfreich?

Lösung

Es kann sogar kürzer, ohne <c:if> getan werden:

<c:forEach items="${learningEntry.samples}" var="sample" varStatus = "status">
    <table class="sampleEntry" ${status.first ? '' : 'style = "display:none"'}> 
</c:forEach> 

Andere Tipps

Ja, varStatus = „stat“ in der foreach-Element deklarieren, so können Sie es fragen, ob es das erste oder das letzte ist. Es ist eine Variable vom Typ LoopTagStatus.

Dies ist die doc für LoopTagStatus: http: //java.sun.com/products/jsp/jstl/1.1/docs/api/javax/servlet/jsp/jstl/core/LoopTagStatus.html Es hat mehr interessante Eigenschaften ...

<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>

Editiert: kopiert von axtavt

Es kann sogar kürzer, ohne <c:if> getan werden:

<c:forEach items="${learningEntry.samples}" var="sample" varStatus = "status">
    <table class="sampleEntry" ${status.first ? '' : 'style = "display:none"'}> 
</c:forEach> 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top