JSTL:迭代列表但以不同方式对待第一个元素

发布于 2024-08-17 01:05:14 字数 547 浏览 2 评论 0原文

我正在尝试使用 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>

I'm trying to process a list using jstl. I want to treat the first element of the list differently than the rest. Namely, I want only the first element to have display set to block, the rest should be hidden.

What I have seems bloated, and does not work.

Thanks for any help.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情愿 2024-08-24 01:05:14

它可以做得更短,无需

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

It can be done even shorter, without <c:if>:

<c:forEach items="${learningEntry.samples}" var="sample" varStatus = "status">
    <table class="sampleEntry" ${status.first ? '' : 'style = "display:none"'}> 
</c:forEach> 
一个人的夜不怕黑 2024-08-24 01:05:14

是的,在 foreach 元素中声明 varStatus="stat",这样您就可以询问它是第一个还是最后一个。它是 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:forEach items="${learningEntry.samples}" var="sample" varStatus = "status">
    <table class="sampleEntry" ${status.first ? '' : 'style = "display:none"'}> 
</c:forEach> 

Yes, declare varStatus="stat" in the foreach element, so you can ask it if it's the first or the last. Its a variable of type LoopTagStatus.

This is the doc for LoopTagStatus:
http://java.sun.com/products/jsp/jstl/1.1/docs/api/javax/servlet/jsp/jstl/core/LoopTagStatus.html
It has more interesting properties...

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

Edited: copied from axtavt

It can be done even shorter, without <c:if>:

<c:forEach items="${learningEntry.samples}" var="sample" varStatus = "status">
    <table class="sampleEntry" ${status.first ? '' : 'style = "display:none"'}> 
</c:forEach> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文