Initial sortorder for PrimeFaces datatable with multisort

后端 未结 1 856
说谎
说谎 2020-12-16 18:16

I am trying to implement multisort on Primeface datatable. We are using Primefaces v3.5. I have created a new load method in the LazyLoadClass that takes the List of SortMet

相关标签:
1条回答
  • 2020-12-16 18:40

    I was able to get this to work.

    Essentially we need to provide the UIColumn in the SortMeta object for it to work. For the initial sort at render time, I had to find the component in my bean and assign that to the sortMeta.

    Below is my code in the view xhtml

            <p:dataTable id="transDataTable" var="trans" 
            value="#{myBean.transModel}" paginator="true" rows="50"
            paginatorAlwaysVisible="false" lazy="true"
            sortMode="multiple" sortBy="#{myBean.preSortOrder}" 
            resizableColumns="true">
    
            <p:column headerText="User" sortBy="#{trans.user.name}" >
                #{trans.user.name}
            </p:column>
            <p:column headerText="Company" sortBy="#{trans.companyName}">
                #{trans.companyName}
            </p:column>
            <p:column headerText="Join Date" id="joinDateTime" 
                sortBy="#{trans.joinDateTime}" >
                <h:outputText value="#{trans.joinDateTime}" />
            </p:column>
        </p:dataTable>
    

    Here is my bean code called on @PostConstruct

            /*
         * method to build initial sort order for multisort
         */
        private void buildSortOrder() {
            UIViewRoot viewRoot =  FacesContext.getCurrentInstance().getViewRoot();
            UIComponent column = viewRoot.findComponent("transDataTable:joinDateTime"); 
    
            SortMeta sm1 = new SortMeta();
            sm1.setSortBy((UIColumn)column);
            sm1.setSortField("joinDateTime");
            sm1.setSortOrder(SortOrder.DESCENDING);
            preSortOrder.add(sm1);          
        }
    

    I am not sure this is the right way to do this, but it works. I am usually uncomfortable when we have to use the ids from view in the bean code, as that can introduce bugs when people are not careful.

    Thanks @CagatayCivici for the quick hint.

    0 讨论(0)
提交回复
热议问题