【问题标题】:Set default sort order for initial header click on gwt cell table为初始标题设置默认排序顺序单​​击 gwt 单元格表
【发布时间】:2012-02-29 18:30:08
【问题描述】:

我有一个 GWT CellTable,其中的可排序列与开发人员指南示例 (http://code.google.com/webtoolkit/doc/latest/DevGuideUiCellTable.html#columnSorting) 非常相似。

但是,我希望某些列默认降序而不是升序。也就是说,如果 A 列当前未排序并且我单击它的标题,我希望 A 列在第一次单击时按降序排序,在第二次单击时按升序排序。但我仍然希望其他列按照目前的方式进行排序 - 在第一次点击时使用升序。

除了设置可排序性的 Column.setSortable(boolean) 方法和手动触发排序的 ColumnSortList.push(ColumnSortInfo) 方法之外,似乎对过程没有太多控制。

有没有办法实现这个目标?

【问题讨论】:

    标签: gwt gwt-celltable


    【解决方案1】:

    Column.setDefaultSortAscending(false) 是执行此操作的简单方法;不需要自定义CellTable

    【讨论】:

    • 看起来这是最近才添加到 api 中的。感谢您的更新。
    【解决方案2】:

    通过覆盖与表关联的ListHandleronColumnSort(ColumnSortEvent) 方法,我能够扩展CellTable 基本上完全符合我的要求。这是实施的肉/土豆。我不得不在幕后进行一些维护,以跟踪当您要再次对列进行排序时是否已对其进行排序。

    让我感到困惑的一件事(在 google 的示例中并不清楚)是使用 ColumnSortList.push() 方法实际上并不会像点击那样触发排序,而只是改变了列的基本状态认为 已排序。

    当我准备使用我制作的这张桌子时,我基本上做了以下事情:

    SortedCellTable<MyRowObject> table = new SortedCellTable<MyRowObject>();
    table.addColumn(colOne, "one", true); // sortable column
    table.addColumn(colTwo, "two", true); //sortable column
    table.addColumn(colThree, "three", false); // unsortable column
    
    table.setDefaultSortOrder(colOne, true); // sorts ascending on first click
    table.setDefaultSortOrder(colTwo, false); // sorts descending on first click
    
    table.setInitialSortColumn(colTwo); // sort this column as soon as data is set
    
    table.setComparator(colOne, colOneComp); // use this comparator when sorting colOne
    table.setComparator(colTwo, colTwoComp); // use this comparator when sorting colTwo
    
    panel.add(table); // add the table to our view
    // ...sometime later, asynchronously...
    table.setList(myRowObjectList);
    

    这是SortedCellTable 的实现:

    public class SortedCellTable<T> extends CellTable<T> {
    /**
     * To keep track of the currently sorted column
     */
    private Column<T, ?> currentlySortedColumn;
    /**
     * Tells us which way to sort a column initially
     */
    private Map<Column<T, ?>, Boolean> defaultSortOrderMap = new HashMap<Column<T, ?>, Boolean>();
    /**
     * Comparators associated with their columns
     */
    private Map<Column<T, ?>, Comparator<T>> comparators = new HashMap<Column<T, ?>, Comparator<T>>();
    /**
     * Column to sort when the data provider's list is refreshed using
     * {@link SortedCellTable#setList(List)}
     */
    private Column<T, ?> initialSortColumn;
    /**
     * Data provider we will attach to this table
     */
    private ListDataProvider<T> dataProvider;
    /**
     * Special column sorting handler that will allow us to do more controlled
     * sorting
     */
    ListHandler<T> columnSortHandler;
    
    public SortedCellTable() {
        super();
        dataProvider = new ListDataProvider<T>();
        dataProvider.addDataDisplay(this);
        columnSortHandler = new ListHandler<T>(dataProvider.getList()) {
    
            @Override
            public void onColumnSort(ColumnSortEvent event) {
                @SuppressWarnings("unchecked")
                Column<T, ?> column = (Column<T, ?>) event.getColumn();
                if (column == null) {
                    return;
                }
    
                if (column.equals(currentlySortedColumn)) {
                    // Default behavior
                    super.onColumnSort(event);
                } else {
                    // Initial sort; look up which direction we need
                    final Comparator<T> comparator = comparators.get(column);
                    if (comparator == null) {
                        return;
                    }
    
                    Boolean ascending = defaultSortOrderMap.get(column);
                    if (ascending == null || ascending) {
                        // Default behavior
                        super.onColumnSort(event);
                    } else {
                        // Sort the column descending
                        Collections.sort(getList(), new Comparator<T>() {
                            public int compare(T o1, T o2) {
                                return -comparator.compare(o1, o2);
                            }
                        });
                        // Set the proper arrow in the header
                        getColumnSortList().push(
                                new ColumnSortInfo(column, false));
                    }
                    currentlySortedColumn = column;
                }
            }
    
            @Override
            public void setComparator(Column<T, ?> column,
                    Comparator<T> comparator) {
                comparators.put(column, comparator);
                super.setComparator(column, comparator);
            }
    
        };
        addColumnSortHandler(columnSortHandler);
    }
    
    /**
     * Adds a column to the table and sets its sortable state
     * 
     * @param column
     * @param headerName
     * @param sortable
     */
    public void addColumn(Column<T, ?> column, String headerName,
            boolean sortable) {
        addColumn(column, headerName);
        column.setSortable(sortable);
        if (sortable) {
            defaultSortOrderMap.put(column, true);
        }
    }
    
    /**
     * Adds a column to the table and sets its sortable state
     * 
     * @param column
     * @param header
     * @param sortable
     */
    public void addColumn(Column<T, ?> column, Header<?> header,
            boolean sortable) {
        addColumn(column, header);
        column.setSortable(sortable);
        if (sortable) {
            defaultSortOrderMap.put(column, true);
        }
    }
    
    /**
     * Sets the column to sort when the data list is reset using
     * {@link SortedCellTable#setList(List)}
     * 
     * @param column
     */
    public void setInitialSortColumn(Column<T, ?> column) {
        initialSortColumn = column;
    }
    
    /**
     * Sets a comparator to use when sorting the given column
     * 
     * @param column
     * @param comparator
     */
    public void setComparator(Column<T, ?> column, Comparator<T> comparator) {
        columnSortHandler.setComparator(column, comparator);
    }
    
    /**
     * Sets the sort order to use when this column is clicked and it was not
     * previously sorted
     * 
     * @param column
     * @param ascending
     */
    public void setDefaultSortOrder(Column<T, ?> column, boolean ascending) {
        defaultSortOrderMap.put(column, ascending);
    }
    
    /**
     * Sets the table's data provider list and sorts the table based on the
     * column given in {@link SortedCellTable#setInitialSortColumn(Column)}
     * 
     * @param list
     */
    public void setList(List<T> list) {
        dataProvider.getList().clear();
        if (list != null) {
            for (T t : list) {
                dataProvider.getList().add(t);
            }
        }
    
        // Do a first-time sort based on which column was set in
        // setInitialSortColumn()
        if (initialSortColumn != null) {
            Collections.sort(dataProvider.getList(), new Comparator<T>() {
    
                @Override
                public int compare(T o1, T o2) {
                    return (defaultSortOrderMap.get(initialSortColumn) ? 1 : -1)
                            * comparators.get(initialSortColumn)
                                    .compare(o1, o2);
                }
    
            });
            // Might as well get the little arrow on the header to make it
            // official
            getColumnSortList().push(
                    new ColumnSortInfo(initialSortColumn, defaultSortOrderMap
                            .get(initialSortColumn)));
            currentlySortedColumn = initialSortColumn;
        }
    }
    }
    

    【讨论】:

      【解决方案3】:

      看看GWT CellTable columnsorting。 (有人投了反对票,因为他/她不同意我的做法?)

      这不是您问题的答案,但它主张您先了解 Celltable 的基础知识,然后再尝试使用 celltable 示例说明的任何内容。

      通过了解单元格/网格表的基本行为和要求,列的排序将清楚地体现出来。

      排序是通过您提供比较器的实现来执行的。就我而言,我尝试了几种实现比较器的方法,以适应我希望对列进行排序的各种方式。

      【讨论】:

      • 感谢您的回复。我在您的链接帖子中同意您的观点,即这些示例没有充分传达使用 CellTable 进行探索所需的功能和方法。我能够继承 CellTable 来做我想做的事。我将作为回复发布。
      • 为了帮助正在寻找答案的人,您应该将您的回复标记为答案。
      • 感谢您的提示。由于我是新人,它希望我等一天才能标记它。
      【解决方案4】:

      而不是

      someTable.getColumnSortList().push(provider.getDefaultSortColumn());
      

      使用

      someTable.getColumnSortList().push(new ColumnSortInfo(provider.getDefaultSortColumn(), false));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多