0

GWTCellTable列仅逐页排序,对于每个页面,我必须单击列标题进行排序。如何在单击标题时对整个数据进行排序。这是我的代码,

   dataProvider = new ListDataProvider<List<NamePair>>();
   dataProvider.addDataDisplay(dgrid);
   List<List<NamePair>> list = dataProvider.getList();

   for (List<NamePair> contact : test) {
      dataProvider.setList(test);
      list.add(contact);
   }


   ListHandler<List<NamePair>> columnSortHandler = new ListHandler<List<NamePair>>(dataProvider.getList());
   System.out.println("Column count->"+dgrid.getColumnCount());

   for(int j=0 ; j<dgrid.getColumnCount();j++){             
        final int val = j;
        columnSortHandler.setComparator(dgrid.getColumn(val), new Comparator<List<NamePair>>() {

        public int compare(List<NamePair> o1, List<NamePair> o2) {
          if (o1 == o2) {
            return 0;
         }

         // Compare the column.
         if (o1 != null) {
            int index = val;
            return (o2 != null) ? o1.get(index-2).compareTo(o2.get(index-2)) : 1;
         }
         return -1;
        }
    });
}

dgrid.addColumnSortHandler(columnSortHandler);
4

1 回答 1

1

我建议你 override ListHandler, override 和 callsuper.onColumnSort(ColumnSortEvent)来调试onColumnSort(ColumnSortEvent)方法,你会很快明白发生了什么。

该方法的源代码非常直接

public void onColumnSort(ColumnSortEvent event) {
  // Get the sorted column.
  Column<?, ?> column = event.getColumn();
  if (column == null) {
    return;
  }

  // Get the comparator.
  final Comparator<T> comparator = comparators.get(column);
  if (comparator == null) {
    return;
  }

  // Sort using the comparator.
  if (event.isSortAscending()) {
    Collections.sort(list, comparator);
  } else {
    Collections.sort(list, new Comparator<T>() {
      public int compare(T o1, T o2) {
        return -comparator.compare(o1, o2);
      }
    });
  }
}
于 2013-10-12T12:24:01.480 回答