3

GWT 2.5 を使用して、並べ替え可能な日付列を持つ CellTable を作成しています。

私のコードは次のとおりです。

CellTable<Activity> table = new CellTable<Activity>();

table.setRowStyles(new RowStyles<Activity>() {
    @Override
    public String getStyleNames(Activity row, int rowIndex) {
        return TABLE_ROW_STYLE_NAME;
    }
});

// create date column
TextColumn<Activity> dateColumn = new TextColumn<Activity>() {
    @Override
    public String getValue(Activity a) {
        return dateFormat.format(a.getDate());
    }
};
dateColumn.setSortable(true);
dateColumn.setDefaultSortAscending(false);
// add column to table
table.addColumn(dateColumn, myConstants.dateColumnHeader());

// attach provider to table
activityProvider.addDataDisplay(table);

// create sort handler
ListHandler<Activity> sortHandler = new ListHandler<Activity>(activityProvider.getList());
sortHandler.setComparator(dateColumn, new Comparator<Activity>() {
    @Override
    public int compare(Activity a1, Activity a2) {
        if (a1 == a2) {
            return 0;
        }

        // compare the date columns
        if (a1 != null) {
            if (a2 != null) {
                long a1Val = a1.getDate().getTime();
                long a2Val = a2.getDate().getTime();
                if (a1Val == a2Val) {
                    return 0;
                }
                else if (a1Val > a2Val) {
                    return 1;
                }
                else {
                    return -1;
                }
            }
            else {
                return 1;
            }
        }

        return -1;
    }
});

// add sort handler to table
table.addColumnSortHandler(sortHandler);

// add date column to table's sort list
table.getColumnSortList().push(dateColumn);

table.setWidth("100%");

getView().getActivityPanel().add(table);

このコードでは、データがテーブルに表示され、列に並べ替え矢印が表示されます。ただし、並べ替え可能な列のヘッダーをクリックしても何も起こりません。並べ替え順序は変更されず、行は再配置されません。

誰でもここで問題を見つけることができますか? このコードは、 Google 独自の例 にあるものとほぼ同じです。

4

2 に答える 2

0

これは私が使用するものです:

    dateColumn.setSortable(true);
    sortHandler.setComparator(dateColumn, new Comparator<ObjectPobject>() {
        public int compare(ObjectPobject o1, ObjectPobject o2) {
            return o1.getDate().compareTo(o2.getDate());
        }
    });
于 2012-08-27T14:20:34.500 に答える
0

そのはず

a1.getDate().getTime().compareTo(a2.getDate().getTime())

また

a1.getDate().after(a2.getDate())

これは、GWT が JavaScript 比較を使用し、compareTo が日付に対して機能しないために発生します。

于 2012-09-13T15:41:00.820 に答える