Primefaces 动态列,以编程方式 [英] Primefaces dynamic columns, programmatically
问题描述
我正在使用 Primefaces 重写一个(非常)大而旧的 Richfaces 应用程序.我现在有一种情况,我得到两个列表,一个包含列名,另一个包含应表示为行的对象.
I'm in the process of rewriting a (very) big and old Richfaces application using Primefaces. I now have a situation where I get two lists, one with column names and one with objects that should be represented as rows.
对象看起来像这样:
public class AVeryGenericThingThatIDoNotLike {
Object[] values;
//Other parameters that I dont care about
}
当我应该显示它时,我希望它将 values[0] 映射到第一列,values[1] 映射到第二列,依此类推.
When I should display this I want it to map values[0] to the first column, values[1] to the second and so forth.
我看过 primefaces 展示,有一个动态列的例子,但我已经尝试过了,当我不知道可以存在哪些列时,我无法让它工作.我不知道这一点的原因是这些列是从一个包含一行的数据库决定的,该行说明应该存在哪些列并且这可以改变,所以我不能像展示示例中那样构建一个类来让所有列都可用.
I have looked at the primefaces showcase and there is an example of dynamic columns but I have tried that and I can not get it to work when I do not know what columns that can exist. The reason that I do not know this is that the columns are decided from a database that holds a row saying what columns that should exist and this can change, so I can not build a class to have all the columns available like in the showcase example.
有人可以在这里给我任何指示吗?我已经坚持了几天,我真的需要帮助.
Can anyone give me any pointers here? I've been stuck on this for a few days and I really need help.
现在它有点使用 Laabidi Raissi 的解决方案.我现在的问题是,对于某些列,我希望呈现按钮和命令链接.使用这种方法,我只能获取字符串,如果我尝试返回像 CommandButtons 这样的对象,它只会打印一个 java 对象哈希.
Now it is kinda working with the solution from Laabidi Raissi. My problem now is that for some columns I want to have buttons being rendered and command links. With this approach I am only able to get strings and if I try to return objects like CommandButtons it will just print a java object hash.
推荐答案
如果您使用的是 JSF2,那么我认为此解决方案可能适合您.首先,在您的托管 bean 中,您从数据库(动态地)构建列列表,然后:
If you are using JSF2, then I think this solution may work for you. First, in your managed bean, you build your list of columns (dynamically) from DB, and then:
<p:columns value="#{yourManagedBean.listOfColumnsFromDB}" var="column" columnIndexVar="colIndex">
<f:facet name="header"> #{column}</f:facet>
<h:outputText value="#{yourManagedBean.values[colIndex]}" />
</p:columns>
根据您的评论,我认为添加 commandbutton
或 outputlink
非常明显(而不是 outputText
):
Base on your comment, I think adding commandbutton
or outputlink
is very obvious (instead of the outputText
):
<h:commandButton value="#{yourManagedBean.values[colIndex]}" action="#{yourManagedBean.someActionThatTakesAParam(colIndex)}" rendered="#{colIndex == 4}"/>
假设您想要第 5 列中的 commandButton.
Supposing you want commandButton in column 5.
这就是你要找的吗?
希望能帮到你
这篇关于Primefaces 动态列,以编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!