2

是否可以从数据表中获取列名作为可读字符串?我需要将它们放在我的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]
4

1 回答 1

2

首先,您应该在列上使用name或至少使用id属性。现在你可以做这样的事情:

Snippet 将打印所有列标题文本,但使用IDs 您将能够以更好的方式获得它

DataTable dataTable = (DataTable) FacesContext.getCurrentInstance()
            .getViewRoot().findComponent("form:userTable");
List<UIColumn> column = dataTable.getColumns();
for (UIColumn uiColumn : column) {
    System.out.println(uiColumn.getHeaderText());  
}  
于 2018-03-28T07:12:19.880 回答