在 bean 中获取 p:dataTable 列名称
Get p:dataTable column names in bean
是否可以从数据 table 中获取列名作为可读字符串?我需要将它们放入我的 LazyDataModel class.
这是我的数据table 和列的图像;
<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]
首先,您应该在列中使用 name
或至少 id
属性。
现在你可以这样做:
Snippet 将打印所有列 header 文本,但使用 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());
}
是否可以从数据 table 中获取列名作为可读字符串?我需要将它们放入我的 LazyDataModel class.
这是我的数据table 和列的图像;
<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]
首先,您应该在列中使用 name
或至少 id
属性。
现在你可以这样做:
Snippet 将打印所有列 header 文本,但使用 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());
}