RichFaces ExtendedTableDataModel:排序列检索所有行 [英] RichFaces ExtendedTableDataModel: sorting columns retrieves all rows

查看:140
本文介绍了RichFaces ExtendedTableDataModel:排序列检索所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用ExtendedTableDataModel进行分页。这是为了使用Hibernate检索一组结果,并在请求另一个页面时加载下一个集合。

We use the ExtendedTableDataModel for paging. This is done to retrieve a set of results with Hibernate and load the next set when another page is requested.

一切工作正常,但是如果我们通过在rich:dataTable中使用rich:column sortBy对列进行排序,则RichFaces会尝试加载所有行。我们可以在调试我们为自定义DataProvider创建的getItemsByRange时看到这一点。点击排序一次后,RichFaces将继续从数据库中检索所有行。这当然不是我们想要的。我们希望使用此解决方案来最大限度地减少数据流量和加载时间。

All works fine, but if we sort columns by using rich:column sortBy within the rich:dataTable then RichFaces tries to load all rows. We can see this while debugging the getItemsByRange which we made for our custom DataProvider. After clicking sort once, RichFaces will continue to retrieve all rows from the database. This is of course not what we want. We wanted to use this solution to minimize the data traffic and load times.

我们使用Richfaces 3.3.2。有没有可能的解决方案或解决方法?

We use Richfaces 3.3.2. Is there someone with a possible solution or workaround?

推荐答案

我们有同样的问题,这里是我们的解决方案。我不完全确定我粘贴的整个代码是相关的,但它应该是

We had the same problem, and here's our solution. I'm not entirely sure that the whole code I'm pasting is relevant, but it should be

public class PagingExtendedTableDataModel<T> extends ExtendedDataModel implements
        Serializable, Modifiable {

    private DataProvider dataProvider;
    private Object rowKey;
    private List wrappedKeys;
    private Map wrappedData;
    private Integer rowCount;
    private Integer rowIndex;
    private List<FilterField> filterFields;
    private List<SortField2> sortFields;

    public PagingExtendedTableDataModel(DataProvider<T> dataProvider) {
        wrappedKeys = null;
        wrappedData = new HashMap();
        rowCount = null;
        rowIndex = Integer.valueOf(-1);
        rowKey = null;

        this.dataProvider = dataProvider;
    }

    public Object getRowKey() {
        return rowKey;
    }

    public void setRowKey(Object key) {
        rowKey = key;
    }

    @SuppressWarnings("unchecked")
    public void walk(FacesContext context, DataVisitor visitor, Range range,
            Object argument) throws IOException {

        int rowC = getRowCount();
        int firstRow = ((SequenceRange) range).getFirstRow();
        int numberOfRows = ((SequenceRange) range).getRows();
        if (numberOfRows <= 0) {
            numberOfRows = rowC;
        }

        if (wrappedKeys != null) {
            Object key;
            for (Iterator it = wrappedKeys.iterator(); it.hasNext();
                visitor.process(context, key, argument)) {

                key = it.next();
                setRowKey(key);
            }

        } else {
            wrappedKeys = new ArrayList();
            int endRow = firstRow + numberOfRows;
            if (endRow > rowC) {
                endRow = rowC;
            }

            if (dataProvider instanceof Sortable2) {
                ((Sortable2) dataProvider).setSortFields(sortFields);
            }

            if (dataProvider instanceof Filterable) {
                ((Filterable) dataProvider).setFilterFields(filterFields);
            }

            Object key;
            for (Iterator it = loadData(firstRow, endRow).iterator(); it.hasNext();
                visitor.process(context, key, argument)) {

                Object item = it.next();
                key = getKey(item);
                wrappedKeys.add(key);
                wrappedData.put(key, item);
            }

        }
    }

    protected List loadData(int startRow, int endRow) {
        if (startRow < 0) {
            startRow = 0;
            throw new IllegalArgumentException((new StringBuilder()).append(
                    "Illegal start index value: ").append(startRow).toString());
        }
        int rowCount = getRowCount();
        if (endRow > rowCount) {
            endRow = rowCount;
            throw new IllegalArgumentException((new StringBuilder()).append(
                    "Illegal end index value: ").append(endRow).toString());
        }

        return dataProvider.getItemsByRange(startRow, endRow);
    }

    public int getRowCount() {
        if (rowCount == null) {
            rowCount = new Integer(dataProvider.getRowCount());
        } else {
            return rowCount.intValue();
        }

        return rowCount.intValue();
    }

    public Object getRowData() {
        if (rowKey == null) {
            return null;
        }

        return getObjectByKey(rowKey);
    }

    @SuppressWarnings("unchecked")
    public Object getKey(Object o) {
        return dataProvider.getKey(o);
    }

    @SuppressWarnings("unchecked")
    public Object getObjectByKey(Object key) {
        Object t = wrappedData.get(key);
        if (t == null) {
            t = dataProvider.getItemByKey(key);
            wrappedData.put(key, t);
        }
        return t;
    }

    public int getRowIndex() {
        return rowIndex.intValue();
    }

    public void setRowIndex(int rowIndex) {
        this.rowIndex = Integer.valueOf(rowIndex);
    }

    public Object getWrappedData() {
        throw new UnsupportedOperationException();
    }

    public void setWrappedData(Object data) {
        throw new UnsupportedOperationException();
    }

    public boolean isRowAvailable() {
        return getRowData() != null;
    }

    public void reset() {
        wrappedKeys = null;
        wrappedData.clear();
        rowCount = null;
        rowIndex = Integer.valueOf(-1);
        rowKey = null;
    }

    public DataProvider getDataProvider() {
        return dataProvider;
    }

    public void setDataProvider(DataProvider dataProvider) {
        this.dataProvider = dataProvider;
    }

    @Override
    public void modify(List<FilterField> filterFields, List<SortField2> sortFields) {
        this.filterFields = filterFields;
        this.sortFields = sortFields;

        reset();
    }
}

还需要一个自定义的 DataProvider

public class PagingDataProvider implements DataProvider<BeanDisplay>,
      Sortable2, Filterable 

在方法 getItemsByRange 有限的记录。所有其他方法应该是直接实现的(我没有粘贴我们的代码,因为它是充满特定的代码,这是不相关的)

In the method getItemsByRange make your query load only a limited amount of records. All other methods should be straightforward to implement (I'm not pasting our code, because it is full of quite specific code, which is not relevant)

你构建你的数据模型如下:

And you construct your data model as follows:

new PagingExtendedTableDataModel<BeanDisplay>(new PagingDataProvider());

这篇关于RichFaces ExtendedTableDataModel:排序列检索所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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