Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

java - Primefaces dynamic columns, programmatically

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.

The object looks like this:

public class AVeryGenericThingThatIDoNotLike {
    Object[] values;
    //Other parameters that I dont care about
}

When I should display this I want it to map values[0] to the first column, values[1] to the second and so forth.

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.

EDIT: 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

EDIT: 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}"/>

Supposing you want commandButton in column 5.

Is that what you are looking for ?

Hope this helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...