没有dataModel的JSF 2 dataTable行索引 [英] JSF 2 dataTable row index without dataModel
问题描述
我一直在使用 ui:repeat 来生成表格.使用 ui:repeat 可以轻松获取使用 varStatus
编号的行.在调用支持 bean 来处理列表导航时,我依赖于 varStatus 中的行 ID,例如在列表中向上或向下移动项目.
I've been using ui:repeat to generate tables. With ui:repeat it's easy to get the rows numbered using the varStatus
. I'm depending on the row id's from varStatus when calling the backing bean to handle list navigation, for example moving an item up or down in the list.
现在我想创建一个复合组件来构建一个可自定义的表格,该表格具有标记行、向上/向下移动它们等所需的所有功能.为此,ui:repeat 将无法工作,因为我不能查看处理不同列数的方法;我需要在一个地方指定标题,在另一个地方指定正文(我还没有达到可以创建自定义组件的知识点).因此,我转而使用数据表,因为使用这意味着将标题指定为与正文内容相同的方面.
Now I'd like to create a composite component that builds a customizable table with all functionality needed for marking rows, moving them up/down etc. To do this, the ui:repeat won't work as I can't see a way of handling different number of columns; I need to specify headings in one place and the body in another (and I've not reached the knowledge point where I can create a custom component). Therefore I went on to datatable instead, as using that means specifying the headers as facets at the same place as the body content.
这很有效,直到我注意到由于某种原因不再有 varStatus
.我已经做了很多工作来处理列表呈现,而无需 datamodel
并且我不想开始使用它.有什么方法可以在不使用 datamodel
的情况下获取当前行号,因为它显示在表中?
That worked well until I noticed that for some reason there were no varStatus
anymore. I have put a lot of work into handling list presentation without any need for a datamodel
and I do not want to start using that. Is there any way that I can get the current row number as it is displayed in the table, without using datamodel
?
此外,我正在使用 viewScope,如果我理解正确,这意味着我无法将 dataTable 绑定到 bean.
Also I'm using viewScope and if I've understood correctly that means I cannot bind the dataTable to the bean.
推荐答案
只需将表绑定到视图本身,而不是绑定到 bean.
Just bind the table to the view itself instead of to a bean.
<h:dataTable binding="#{table}" ...>
然后您可以在必要时使用 #{table.rowIndex}
.例如
Then you can use #{table.rowIndex}
where necessary. E.g.
<h:column>#{table.rowIndex + 1}</h:column>
请注意代码是原样的,EL 变量名称 table
完全由您选择.
Note that the code is as-is and that the EL variable name table
is fully to your choice.
这篇关于没有dataModel的JSF 2 dataTable行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!