0
votes

I know how to sort the TextColumn of cellTable in GWT.

That is setSortable(true) for textColumn

then declare ListHandler<String[]> columnSortHandler = new ListHandler<String[]>(myTableList);

then columnSortHandler.setComparator(textColumn, new MyCellTableColumnComparator(1));,

finally getView().getMyCellTable().addColumnSortHandler(columnSortHandler);

But what if we have CheckBox column & we want to sort CheckBox column. That is when user clicks the header of CheckBox column it will bring all the rows that has CheckBox checked to top. If user clicks the header of CheckBox column again it will bring all the rows that has CheckBox unchecked to top. So there are only 2 possible options for sorting CheckBox column, that is checked & unchecked.

We declare CheckBox column as following:

Column<String[], Boolean> checkBoxColumn = new Column<String[], Boolean>(
                        new CheckboxCell(true, false)) {
  @Override
  public Boolean getValue(String[] object) {
      return mySelectionModel.isSelected(object);
  }
};

ckColumn.setSortable(true);

But then how can we setComparator for checkBoxColumn?

Or can you find a better solution?

1
And what exactly is the problem with setting a comparator on checkbox column?Andrei Volgin
Ok, I got it, seem the checked box has no influence on the sort, so I just compare checkBoxColumn just like I do with TextColumn & it works ok. Anyway, thax you for your hint.Tum

1 Answers

0
votes

I wrote an example table, which should work out of the box :

public class ExampleTable extends CellTable<MyObject> {

private ArrayList<MyObject> list;
private ListDataProvider<MyObject> dataProvider = new ListDataProvider<MyObject>();

public ExampleTable() {
    super();
    dataProvider.addDataDisplay(this);

    list = new ArrayList<MyObject>();
    for (int i = 0; i < 15; i++) {
        list.add(new MyObject("Value" + i, false));
    }

    dataProvider.setList(list);

    TextColumn<MyObject> textColumn = new TextColumn<MyObject>() {

        @Override
        public String getValue(MyObject object) {
            return object.getValue();
        }
    };
    this.addColumn(textColumn, "String");

    Column<MyObject, Boolean> checkBoxColumn = new Column<MyObject, Boolean>(
            new CheckboxCell(true, true)) {
        @Override
        public Boolean getValue(MyObject object) {

            return object.isEnabled();
        }
    };

    checkBoxColumn.setFieldUpdater(new FieldUpdater<MyObject, Boolean>() {

        @Override
        public void update(int index, MyObject object, Boolean value) {
            object.setEnabled(value);
        }
    });

    this.addColumn(checkBoxColumn, "Boolean");
    checkBoxColumn.setSortable(true);

    ListHandler<MyObject> columnSortHandler = new ListHandler<MyObject>(
            dataProvider.getList());

    columnSortHandler.setComparator(checkBoxColumn,
            new Comparator<MyObject>() {

                @Override
                public int compare(MyObject arg0, MyObject arg1) {
                    return (arg0.isEnabled() == arg1.isEnabled() ? 0
                            : (arg1.isEnabled() ? 1 : -1));
                }
            });
    this.addColumnSortHandler(columnSortHandler);

}

  };

The MyObject is just a simple Pojo :

public class MyObject {
private String value;
private boolean enabled;

public MyObject(String value, boolean status) {
    this.value = value;
    this.enabled = status;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public boolean isEnabled() {
    return enabled;
}

public void setEnabled(boolean status) {
    this.enabled = status;
}

}

It's important to use a filled list when creating the ListHandler. If you don't want updateable boolean column, just remove the FieldUpdater.