如何使用ScrollTable实现自定义ColumnSorter(GWT-incubator)

时间:2009-09-04 14:10:49

标签: java sorting gwt

我已经实现了我的自定义列分类器,用于对其进行排序 我桌子上的元素。

    class FileColumnSorter extends SortableGrid.ColumnSorter
    {
        @Override
        public void 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();

// Calling the lines described above
FixedWidthGrid fileListGrid = createDataTable
(currentDescriptorList.size(), 6);

// Combine the components into a 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) {
    @Override
    public ColumnSorter getColumnSorter()
    {
        return sorter;
    }
};

为确保我的分拣机得到使用,但我仍然保持同样的状态 经验。

更新:添加了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);
    }
}

1 个答案:

答案 0 :(得分:1)

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