i'm using primefaces 4.0, JSF Mojarra 2.2.2 and here is my datatable code :
<p:dataTable id="tabexam"
paginatorPosition="bottom"
var="exam"
value="#{dyna.examViewDataModel}"
widgetVar="examTable"
emptyMessage="aucun résultat trouvé pour votre recherche"
paginator="true"
rows="40"
selection="#{dyna.selectedExamen}"
filteredValue="#{dyna.filteredexams}"
selectionMode="single"
resizableColumns="true"
draggableColumns="true"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="40,80,120">
<p:columns value="#{datatableBean.table}"
var="column"
headerText="#{column.userListname}"
rendered="#{column.visible}"
resizable="#{column.resizable}"
width="#{column.width}"
columnIndexVar="colIndex">
<h:panelGroup>
<p:outputLabel value="#{column.dbname}" styleClass="fonty"/>
</h:panelGroup>
</p:columns>
</p:dataTable>
Here what I've tried so far, but I get empty Strings for getHeaderText in the console and empty width too, i wonder what i'm missing
UIViewRoot view = FacesContext.getCurrentInstance().getViewRoot();
DataTable tabler = (DataTable) view.findComponent(":form1:tabexam");
List<UIColumn> coler = tabler.getColumns();
for (int i = 0; i < coler.size(); i++) {
System.out.println("/////////////////");
System.out.println(coler.get(i).getValueExpression("headerText").getValue(FacesContext.getCurrentInstance().getELContext()));
System.out.println(coler.get(i).getHeaderText());
System.out.println(coler.get(i).isRendered());
System.out.println(coler.get(i).isResizable());
System.out.println(coler.get(i).getWidth());
System.out.println("/////////////////");
}
System.out.println(coler.size());
Note that coler.size()
gives the number of columns shown on the datatable.
but the coler.get(i).getHeaderText()
always gives empty String.
you are mistaking something:
p:column
insidep:columns
DataTable.getColumns does not return what you expect:
this returns a synthetic emulation: only
columnKey
per column, and, as you stated, the correct number of columns.generally speaking,
p:columns
is ONE component and when you access it from controller layer, you have to refer to its DECLARATION (single component) instead of its REPRESENTATION (multiple columns)p:columns
with your#{datatableBean.table}
you should already have all the information you are trying to get from DataTable component, in a reverse-engineered fashion.update
oh, now i realize what you want to do. you have to use event listeners for that.
from PF showcase:
listening for these events in backing bean and mantain these informations should solve your problem
update 2
whew, it was not simple at all.
due to Primefaces bug for dynamic column resizing (at least for my tries) and incomplete column reordering event implementation, this is what came out:
and this bean:
updated code, you can find ordered columns in
columnList2
. now are also shown in the form. now you can usecolumnList2
to store oreder and widths to db.