Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

jsf 2 - Initial sortorder for PrimeFaces datatable with multisort

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 SortMeta> parameter.

But I am having issues in the initial load of the table. The List of SortMeta> is null when the load method is called. I have also tried without specifying the initial sortBy and sortOrder for the datatable. In both the cases the result is the same.

Seeing that we have this new class SortMeta to support multisort, I suspect that the way to specify the initial sort field and order would have also changed. But I couldn't find any example anywhere to point the difference. The manual 3.5 doesn't mention any difference.

Why we could be getting the List of SortMeta> as null? Any pointers on example code where multisort is used with Lazyload?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...