Reputation: 45
Is it possible to get column names as a readable string from data table? I need to get them in my LazyDataModel class.
Here's the image of my datatable and columns;
<h:body styleClass="login">
<h:form id="form">
<p:dataTable var="users" value="#{userBean.users}" paginator="true"
widgetVar="usersTable" rows="10" sortMode="multiple"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
rowsPerPageTemplate="5,10,15" selectionMode="single" id="userTable"
lazy="true">
<f:facet name="header">
<p:outputPanel style="text-align: right">
<h:outputText value="Search all fields:" />
<p:inputText id="globalFilter" onkeyup="PF('usersTable').filter()"
style="width:150px" placeholder="Enter keyword" />
</p:outputPanel>
</f:facet>
<p:column headerText="Id" sortBy="#{users.id}"
filterBy="#{users.id}">
<h:outputText value="#{users.id}" />
</p:column>
<p:column headerText="Login" sortBy="#{users.login}"
filterBy="#{users.login}">
<h:outputText value="#{users.login}" />
</p:column>
<p:column headerText="Firstname" sortBy="#{users.firstName}"
filterBy="#{users.firstName}">
<h:outputText value="#{users.firstName}" />
</p:column>
<p:column headerText="Lastname" sortBy="#{users.lastName}"
filterBy="#{users.lastName}">
<h:outputText value="#{users.lastName}" />
</p:column>
<p:column headerText="DayOfBirth" sortBy="#{users.dayOfBirth}"
filterBy="#{users.dayOfBirth}">
<h:outputText value="#{users.dayOfBirth}" />
</p:column>
<p:column headerText="District" sortBy="#{users.district}"
filterBy="#{users.district}">
<f:facet name="filter">
<p:selectOneMenu onchange="PF('usersTable').filter()" id="filter">
<f:selectItem itemLabel="Select One" itemValue="#{null}"
noSelectionOption="true" />
<f:selectItems value="#{userBean.districts}" />
</p:selectOneMenu>
</f:facet>
<h:outputText value="#{users.district}" />
</p:column>
<p:column headerText="City" sortBy="#{users.city}"
filterBy="#{users.city}">
<f:facet name="filter">
<p:selectCheckboxMenu label="Select one"
onchange="PF('usersTable').filter()" panelStyle="width:170px"
scrollHeight="150">
<f:selectItems value="#{userBean.cities}" />
</p:selectCheckboxMenu>
</f:facet>
<h:outputText value="#{users.city}" />
</p:column>
</p:dataTable>
</h:form>
</h:body>
Here's what I did and how I printed them;
DataTable dataTable = (DataTable) FacesContext.getCurrentInstance()
.getViewRoot().findComponent("form:userTable");
System.out.println(dataTable.getColumns());
I actually get the array of column names but the their type is like;
[org.primefaces.component.column.Column@1543c5e7,
org.primefaces.component.column.Column@32434af6,
org.primefaces.component.column.Column@6f84a39b,
org.primefaces.component.column.Column@6b30f4d2,
org.primefaces.component.column.Column@605768ae,
org.primefaces.component.column.Column@48eb8aa8,
org.primefaces.component.column.Column@2b97055]
Upvotes: 2
Views: 2340
Reputation: 1441
First you should use name
or at least id
attributes on your columns.
And for now you can do something like this:
Snippet will print all column header texts but with using ID
s you will be able to get it a better way
DataTable dataTable = (DataTable) FacesContext.getCurrentInstance()
.getViewRoot().findComponent("form:userTable");
List<UIColumn> column = dataTable.getColumns();
for (UIColumn uiColumn : column) {
System.out.println(uiColumn.getHeaderText());
}
Upvotes: 2