Html html表格行的行计数器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1979426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:43:05  来源:igfitidea点击:

row counter for html table row

htmljsfrichfacesjstl

提问by crazyTechie

I have HTML table in JSF web application. I am generating rows dynamically using <ui:repeat>. I want a counter for each row. How can I get this? Any help?

我在 JSF Web 应用程序中有 HTML 表。我正在使用<ui:repeat>. 我想要每行一个计数器。我怎样才能得到这个?有什么帮助吗?

Ssimilar to rowKeyVar in rich faces dataTable.

类似于富人脸数据表中的rowKeyVar。

回答by ustun

As of Facelets 2.0, this is now possible using the varStatus.

从 Facelets 2.0 开始,现在可以使用varStatus.

<ui:repeat varStatus="status" var="user" value="#{collection}">
     #{status.index}
</ui:repeat>

回答by Bozho

Since you are using richfaces, you can do it with its iteration tag (<a4j:repeat>), which is a bit more appropriate than using <c:forEach>, and which is like an extension to <ui:repeat>

由于您使用的是 Richfaces,您可以使用它的迭代标记 ( <a4j:repeat>) 来实现,这比 using 更合适<c:forEach>,并且类似于对<ui:repeat>

<table>
    <a4j:repeat value="#{bean.list}" var="item" rowKeyVar="idx">
        <tr>
            <td><h:outputText value="#{idx + 1}" /></td>
            <td><h:outputText value="#{item.someProperty}" /></td>
            <td><h:outputText value="#{item.otherProperty}" /></td>
        </tr>
    </a4j:repeat>
</table>

回答by BalusC

You can't do it nicely with the ui:repeat, but you can do so with the h:dataTable. Bind the component to a UIDataproperty in the backing bean. It has a getRowIndex()method which does exactly what you need:

你不能用 很好地做到这一点ui:repeat,但你可以用 做到这一点h:dataTable。将组件绑定到UIData支持 bean 中的属性。它有一种getRowIndex()方法可以完全满足您的需要:

<h:dataTable binding="#{bean.table}" value="#{bean.list}" var="item">
    <h:column><h:outputText value="#{bean.table.rowIndex + 1}" /></h:column>
    <h:column><h:outputText value="#{item.someProperty}" /></h:column>
    <h:column><h:outputText value="#{item.otherProperty}" /></h:column>
</h:dataTable>

Here I am adding 1to UIData#getRowIndex()because it is zero-based. You may find the Using datatablesarticle useful to learn more about datatables.

我在这里添加1UIData#getRowIndex()因为它是基于零的。您可能会发现使用数据表文章有助于了解有关数据表的更多信息。

If you actually want a bit more control over the table layout (especially using colspans/rowspans, which are lacking in the real JSF h:dataTabletag), then an alternative is to use the JSTL c:forEachtag which has a varStatusattribute which gives you a handle to the loop status.

如果你真的想要对表格布局有更多的控制(特别是使用 colspans/rowspans,它们在真正的 JSFh:dataTable标签中是缺乏的),那么另一种选择是使用 JSTLc:forEach标签,它有一个varStatus属性,可以为你提供循环句柄地位。

<table>
    <c:forEach items="#{bean.list}" var="item" varStatus="loop">
        <tr>
            <td><h:outputText value="#{loop.index + 1}" /></td>
            <td><h:outputText value="#{item.someProperty}" /></td>
            <td><h:outputText value="#{item.otherProperty}" /></td>
        </tr>
    </c:forEach>
</table>

回答by valli

There is no counter for each row in ui:repeat. As BalusC said you can go for h:datatable. Another idea is indirectly you can add a additional index method in bean for each object in the list in serverside and get that in jsf and manipulate it.

ui:repeat 中的每一行都没有计数器。正如 BalusC 所说,您可以选择 h:datatable。另一个想法是间接地,您可以在 bean 中为服务器端列表中的每个对象添加一个额外的索引方法,并在 jsf 中获取它并对其进行操作。

回答by BachT

A bad (or good idea) is using JavaScriptto do that trick.

一个坏(或好主意)是使用JavaScript来做这个把戏。

<script>
    var numberOfIndex = 0;
</script>
<ui:repeat value="#{modelBean.listUser}" var="item">
    <tr>
            <td><script>numberOfIndex -=-1;document.write(numberOfIndex);</script></td>
        <td>${item.id}</td>
        <td>${item.name}</td>
    </tr>
</ui:repeat>