【问题标题】:How to implement a custom ColumnSorter with ScrollTable (GWT-incubator)如何使用 ScrollTable(GWT 孵化器)实现自定义 ColumnSorter
【发布时间】:2016-10-28 03:26:13
【问题描述】:

我已经实现了我的自定义列排序器,用于对 我表中的元素。

类 FileColumnSorter 扩展 SortableGrid.ColumnSorter { @覆盖 公共无效 onSortColumn(SortableGrid sortableGrid,TableModelHelper.ColumnSortList columnSortList, SortableGrid.ColumnSorterCallback columnSorterCallback) …… }

当我初始化 FixedWidthGrid 时,我会执行以下操作:

FixedWidthGrid dataTable = new FixedWidthGrid(rows, cols); dataTable.setSelectionPolicy(SelectionGrid.SelectionPolicy.ONE_ROW); dataTable.setColumnSorter(new FileColumnSorter());

滚动表的初始化方式如下:

FixedWidthFlexTable headerTable = createHeaderTable(); // 调用上述行 FixedWidthGrid fileListGrid = createDataTable (currentDescriptorList.size(), 6); // 将组件组合成一个 ScrollTable scrollTable = new ScrollTable(fileListGrid, headerTable); scrollTable.setSortPolicy(AbstractScrollTable.SortPolicy.SINGLE_CELL); scrollTable.setColumnSortable(0, false); scrollTable.setColumnSortable(1, true); scrollTable.setColumnSortable(2, true); scrollTable.setColumnSortable(3, true); scrollTable.setColumnSortable(4, true); scrollTable.setColumnSortable(5, false);

当我运行应用程序时,我得到的是内置排序而不是我的 自定义排序。我也尝试过以下操作:

ColumnSorter sorter = new FileColumnSorter(); FixedWidthGrid dataTable = new FixedWidthGrid(rows, cols) { @覆盖 公共 ColumnSorter getColumnSorter() { 返回分拣机; } };

为了确保我的分拣机得到使用,但我仍然得到相同的结果 经验。

更新:添加了 FileColumnSorter

class FileColumnSorter extends SortableGrid.ColumnSorter
{
    @Override
    public void onSortColumn(SortableGrid sortableGrid,
        TableModelHelper.ColumnSortList columnSortList,
        SortableGrid.ColumnSorterCallback columnSorterCallback)
    {
        final int column = columnSortList.getPrimaryColumn();

        final Integer[] originalOrder = new Integer[sortableGrid.getRowCount()];
        for (int i = 0; i < originalOrder.length; i++)
        {
            originalOrder[i] = i;
        }

        Arrays.sort(originalOrder, new Comparator<Integer>() {
            public int compare(Integer first, Integer second)
            {
                Descriptor firstDesc = share.getCurrentDescriptors().get(first);
                Descriptor secondDesc = share.getCurrentDescriptors().get(second);

                if (firstDesc.getType().equals(secondDesc.getType()))
                {
                    switch (column)
                    {
                        case 0:
                            return firstDesc.compareTo(secondDesc);
                        case 1:
                            return firstDesc.getName().compareTo(secondDesc.getName());
                        case 2:
                            return ((Long) firstDesc.getSize()).compareTo(secondDesc.getSize());
                        case 3:
                            return firstDesc.getCreated().compareTo(secondDesc.getCreated());
                        case 4:
                            return firstDesc.getModified().compareTo(secondDesc.getModified());
                        default:
                            return firstDesc.compareTo(secondDesc);
                    }
                }
                else
                {
                    return firstDesc.getType() == Descriptor.FileItemType.FOLDER ? 1 : -1;
                }
            }
        });

        int[] resultOrder = new int[originalOrder.length];
        for (int i = 0; i < originalOrder.length; i++)
        {
            if (columnSortList.isPrimaryAscending())
            {
                resultOrder[i] = originalOrder[i];
            }
            else
            {
                resultOrder[resultOrder.length - i - 1] = originalOrder[i];
            }
        }
        columnSorterCallback.onSortingComplete(resultOrder);
    }
}

【问题讨论】:

  • 这种模式适合我。可以添加FileColumnSorter的代码吗?
  • 已经更新了排序器的实现。这包括一个描述符,它是一个带有一些基本字段的 DTO。
  • 愚蠢的问题 - 您在发布的代码中将 FileColumnSort 作为类名,但实例化 FileColumnSorter。以防万一您实际上有两个不同的类意外运行并实例化了错误的类...
  • 不——只是我在这里写错了。

标签: java sorting gwt


【解决方案1】:

我发现了这个关于如何将 PagingScrollTable 与 GWT 孵化器一起使用的好例子。也许您可以使用它而不是自己实现:http://zenoconsulting.wikidot.com/blog:17

【讨论】:

    猜你喜欢
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多