I have Search form. After clicking on Search button data refreshes, but dataTable columns not (columns get refreshed only on second button click). Apparently I am doing something wrong. Dynamic column code is inspired by PrimeFaces Showcase
myForm.xhtml:
<h:form id="MY_FORM">
#{myBean.initBean()}
...
<h:panelGrid id="main">
<h:panelGrid id="buttons">
<p:commandButton id="submitSearch"
value="#{msg['button.execute']}"
actionListener="#{myBean.submitSearch}"
update="resultPanel"/>
</h:panelGrid>
</h:panelGrid>
<h:panelGrid id="resultPanel" border="0">
<p:dataTable id="resultTable" var="result" value="#{myBean.searchResults}">
<p:columns value="#{myBean.columns}" var="column" columnIndexVar="colIndex">
<f:facet name="header">
<h:outputText value="#{column.header}" />
</f:facet>
<h:outputText value="#{result[column.property]}" />
</p:columns>
</p:dataTable>
</h:panelGrid>
</h:form>
I've called column calculation method createColumns() from myBean.initBean() and myBean.submitSearch() with the same result (I see it works correctly from debugger).
@ManagedBean("myBean")
@Scope(value = "session")
public class MyBean {
private ArrayList<HashMap<String,Object>> searchResults;
private List<ColumnModel> columns;
...
public void initBean() {
...
createColumns(selectedDates);
}
public void submitSearch() {
...
ArrayList<HashMap<String, Object>> results = prpRepository.getSearchResultByParams(searchParams);
createColumns(selectedDates);
}
private void createColumns(ArrayList<String> selectedDates){
columns = new ArrayList<ColumnModel>();
columns.add(new ColumnModel("Name", "NAME"));
columns.add(new ColumnModel("Amount", "AMOUNT"));
for (int i = 0; i < selectedDates.size(); i++) {
columns.add(new ColumnModel(selectedDates.get(i), "DATE" + i));
}
setColumns(columns);
}
public List<ColumnModel> getColumns() {
return columns;
}
public void setColumns(List<ColumnModel> columns) {
this.columns = columns;
}
}
Additional information: I am using:
- Oracle's Implementation of the JSF 2.1 Specification
- JavaServer Pages API 2.2
- Springframework 3.1.2 (incl. spring-context, spring-web)
- Glassfish Javax.annotation 3.1.1
searchResults. In the example, it's working with an object list (car) while you're using a hashmap list. In your datatable, thevarattribute is a hashmap, isn't it? - Miguel<h:form id="MY_FORM">#{myBean.initBean()}</h:form>? This doesn't make sense. Get rid of it and use a@PostConstructannotated method as shown here: stackoverflow.com/questions/5765853/… And make sure the bean is view scoped. Perhaps it solves all side effects of doing this. - BalusCcreateColumns(). It would make sense if you do that after you have obtainedselectedDatessomehow, most likely insubmitSearch(). If you're creating columns before you've obtainedselectedDates, then well, there would logically indeed be no columns in first place. - BalusC