本文介绍了具有多重排序的 PrimeFaces 数据表的初始排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Primeface 数据表上实现多重排序.我们使用的是 Primefaces v3.5.我在 LazyLoadClass 中创建了一个新的加载方法,它采用 List of SortMeta> 参数.

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.

但是我在表格的初始加载中遇到了问题.调用load方法时,SortMeta>的List为null.我也尝试过不指定数据表的初始 sortBy 和 sortOrder.在这两种情况下,结果是相同的.

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.

看到我们有这个新类 SortMeta 来支持多重排序,我怀疑指定初始排序字段和顺序的方式也会改变.但我在任何地方都找不到任何例子来指出差异.手册 3.5 没有提到任何区别.

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.

为什么我们可以将 SortMeta 列表设为 null?有关于将多重排序与 Lazyload 一起使用的示例代码的任何指针吗?

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

推荐答案

我能够让它发挥作用.

本质上,我们需要在 SortMeta 对象中提供 UIColumn 以使其工作.对于渲染时的初始排序,我必须在 bean 中找到组件并将其分配给 sortMeta.

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.

下面是我在 xhtml 视图中的代码

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>

这是我在@PostConstruct 上调用的 bean 代码

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);
    }

我不确定这是执行此操作的正确方法,但它确实有效.当我们必须在 bean 代码中使用视图中的 id 时,我通常会感到不舒服,因为当人们不小心时,这可能会引入错误.

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.

感谢@CagatayCivici 的快速提示.

Thanks @CagatayCivici for the quick hint.

这篇关于具有多重排序的 PrimeFaces 数据表的初始排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 07:44