是否可以从数据表中获取列名作为可读字符串?我需要在 LazyDataModel 类中获取它们。
这是我的数据表和列的图像;
<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>
这是我做的以及我如何打印它们;
DataTable dataTable = (DataTable) FacesContext.getCurrentInstance()
.getViewRoot().findComponent("form:userTable");
System.out.println(dataTable.getColumns());
我实际上得到了列名的数组,但它们的类型是;
[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]
答案 0 :(得分:2)
首先,您应该在列上使用name
或至少id
个属性。
现在你可以这样做:
Snippet将打印所有列标题文本,但使用ID
s您将能够更好地获取它
DataTable dataTable = (DataTable) FacesContext.getCurrentInstance()
.getViewRoot().findComponent("form:userTable");
List<UIColumn> column = dataTable.getColumns();
for (UIColumn uiColumn : column) {
System.out.println(uiColumn.getHeaderText());
}