3
votes

I am using GWT 2.5 to create a CellTable with a sortable date column.

My code is as follows:

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);

With this code, the data is displayed in the table and the sorting arrow on the column appears. However, nothing happens when I click on the sortable column's header. The sorting order doesn't change, rows are not rearranged.

Can anyone spot the problem here? This code is almost the same as what they have in Google's own example.

2
I have the same problem and guess it's related to loaded jQuery Mobile JS?! if I call table.getColumnSortList().push( someStringColumn ) it displays the table sorted descending on the column. On column header mouse selection there is some selection border drawn around header and body columns and the sort-icon is not shown. Clicking in the body area then removes this selection, shows the correct sort direction indicator change, but does not sort the table :-(Andreas Covidiot
neither Comparator.compare(...) nor Column.getValue(...) are called after having added some debugging :-/ ... but getValue(...) is called on tbody row cell clicks.Andreas Covidiot

2 Answers

0
votes

This is what I use:

    dateColumn.setSortable(true);
    sortHandler.setComparator(dateColumn, new Comparator<ObjectPobject>() {
        public int compare(ObjectPobject o1, ObjectPobject o2) {
            return o1.getDate().compareTo(o2.getDate());
        }
    });
0
votes

It should be

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

or

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

This happens because GWT uses JavaScript comparisons, and compareTo does not work for dates.