是否可以对Datagrid进行排序? [英] is it possible to sort primefaces Datagrid?

查看:162
本文介绍了是否可以对Datagrid进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可能排序原始datagrid数据?因为我知道在数据表中是可能的。如果默认情况下是不可能的,有没有其他的方法呢?谢谢

is it possible sort primefaces datagrid data? as i'm aware it's possible in data table.if it is not possible by default,is there any other way to do it? Thanks

推荐答案

原文data​​grid不提供排序和过滤,似乎没有计划在任何时候实现它。 br />
我已经在datagrid中使用ap:selectOneMenu在datagrid上实现了排序,其中包含Date,Price(最先),Price(最低优先)等排序选项。所选值更改,我使用ajax事件来调用一个方法,使用适当的ORDER BY从数据库重新加载集合。

Primefaces datagrid does not provide sorting nor filtering, and it seems that there are no plans for implementing it anytime soon.
I've implemented sorting in a datagrid using a p:selectOneMenu just above the datagrid with sorting options like "Date", "Price (highest first)", "Price (lowest first)", etc. When the selected value changes, I use an ajax event to invoke a method that reloads the collection from database using the appropiate ORDER BY.

示例:

<h:form>
    <h:outputLabel value="Sort by: " />
    <p:selectOneMenu value="#{bean.selectedField}" >
        <f:selectItems value="#{bean.sortingFields}" />
        <p:ajax event="change" update="grid" />
    </p:selectOneMenu>

    <p:dataGrid id="grid" value="#{bean.collection}" var="item" >
            <!-- contents of datagrid here -->
    </p:dataGrid>
</h:form>

在托管bean中:

private List<SelectItem> sortingFieds;
private String selectedField, currentSortField;
private boolean asc;
private List<YourEntity> collection;
@EJB private YourEJB yourEjb;

public void init() {
    // load sortingFields with list of fields to order by
    // set default value for currentSortField
    asc = true;
    collection = yourEjb.loadCollection(sortField, asc);
}

public void sortCollection() {
    if(currentSortField.equals(selectedField) {
        asc = !asc;
    } else {
        currentSortField = selectedField;
        asc = true;
    }
    collection = yourEjb.loadCollection(sortField, asc);
}

在您的ejb中

public List<YourEntity> loadCollection(String sortfield, boolean asc) {
    String q = "SELECT e FROM YourEntity e ORDER BY " + sortfield + " ";
    if(asc) {
       q += "ASC";
     } else {
       q += "DESC";
    }
    Query query = entityManager.createQuery(q);
    return query.getResultList();
}

这篇关于是否可以对Datagrid进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆