3

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.

Chania
  • 307
  • 3
  • 14
  • 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 Feb 08 '15 at 02:29
  • neither `Comparator.compare(...)` nor `Column.getValue(...)` are called after having added some debugging :-/ ... but `getValue(...)` is called on tbody row cell clicks. – Andreas Covidiot Feb 08 '15 at 02:51

2 Answers2

0

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());
        }
    });
Ben
  • 6,107
  • 6
  • 29
  • 40
  • this is same comparator as above, except that you are not checking for null. one question: what version of gwt are you using? i think column sorting may be broken in 2.5rc1 – Chania Sep 11 '12 at 15:15
0

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.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58