2012-02-29 11 views
7

Mam GWT CellTable z sortowalnymi kolumnami bardzo podobnymi do przykładu prowadnika programisty (http://code.google.com/webtoolkit/doc/latest/DevGuideUiCellTable.html#columnSorting).Ustaw domyślną kolejność sortowania dla początkowego nagłówka kliknij na tabelę komórek gwt

Jednak chciałbym, aby niektóre kolumny sortowały malejąco, a nie domyślnie. Oznacza to, że jeśli kolumna A nie jest obecnie posortowana i kliknę na jej nagłówek, chcę, aby kolumna A była sortowana malejąco po pierwszym kliknięciu i rosnąco po drugim kliknięciu. Ale nadal chcę, aby inne kolumny były sortowane tak, jak obecnie - za pomocą pierwszego zamówienia, w porządku rosnącym.

Oprócz metody Column.setSortable (Boolean), aby ustawić sortability oraz metodę ColumnSortList.push (ColumnSortInfo) ręcznie wywołać pewnego rodzaju, nie wydaje się być dużo kontroli nad procesem.

Czy istnieje sposób na osiągnięcie tego celu?

Odpowiedz

7

Column.setDefaultSortAscending(false) jest łatwy sposób to zrobić to; nie jest wymagany niestandardowy CellTable.

+0

Wygląda na to, że właśnie zostało dodane do api. Dziękuję za aktualizację. – ahaurw01

+0

Został dodany w gwt 2.5 – Helpa

2

Spójrz na GWT CellTable columnsorting. (Ktoś odmówił, ponieważ nie zgodził się z moim podejściem?)

To nie jest odpowiedź na twoje pytanie, ale zaleca, abyś zapoznał się z podstawami Celltable przed próbą czegokolwiek zilustrowanego przykładami z komórkami.

Dzięki zrozumieniu podstawowego zachowania i wymagań tabel Cell/Grid, sortowanie kolumn przejawia się wyraźnie.

Sortowanie odbywa się poprzez dostarczenie implementacji komparatora. Z mojej strony próbowałem kilku sposobów implementacji komparatora, aby dopasować różne sposoby sortowania kolumn.

+0

Dzięki za odpowiedź. Zgadzam się z Tobą w twoim połączonym poście, że przykłady nie przekazują w wystarczającym stopniu możliwości i podejścia do tangoing z CellTable. Udało mi się podklasować CellTable, aby zrobić to, co chciałem.Wpisuję jako odpowiedź. – ahaurw01

+0

Aby pomóc osobom, które szukają odpowiedzi, należy oznaczyć odpowiedź jako odpowiedź. –

+0

Dziękuję za napiwek. Ponieważ jestem nowy, chcę, żebym poczekał dzień, zanim go oznaczy. – ahaurw01

5

udało mi się przedłużyć CellTable zrobić dokładnie to, co chciałem w zasadzie poprzez nadpisanie sposobu ListHandler związanego ze stołem onColumnSort(ColumnSortEvent). To jest mięso/ziemniaki z wdrożenia. Musiałem zrobić trochę konserwacji pod kołdrą, aby śledzić, czy kolumna została posortowana, gdy masz zamiar ją ponownie sortować.

Jedną rzeczą, która mylić mnie (i nie było jasne, w przykładach Google'a) było to, że stosując metodę ColumnSortList.push() faktycznie nie wywołać pewnego rodzaju jak kliknięcie robi, ale zamiast po prostu zmienia podstawowy stan jak kolumna myśli to jest posortowane.

Kiedy byłem gotowy do wykorzystania tej tabeli zrobiłem, ja w zasadzie nie następuje:

SortedCellTable<MyRowObject> table = new SortedCellTable<MyRowObject>(); 
table.addColumn(colOne, "one", true); // sortable column 
table.addColumn(colTwo, "two", true); //sortable column 
table.addColumn(colThree, "three", false); // unsortable column 

table.setDefaultSortOrder(colOne, true); // sorts ascending on first click 
table.setDefaultSortOrder(colTwo, false); // sorts descending on first click 

table.setInitialSortColumn(colTwo); // sort this column as soon as data is set 

table.setComparator(colOne, colOneComp); // use this comparator when sorting colOne 
table.setComparator(colTwo, colTwoComp); // use this comparator when sorting colTwo 

panel.add(table); // add the table to our view 
// ...sometime later, asynchronously... 
table.setList(myRowObjectList); 

Oto realizacja SortedCellTable:

public class SortedCellTable<T> extends CellTable<T> { 
/** 
* To keep track of the currently sorted column 
*/ 
private Column<T, ?> currentlySortedColumn; 
/** 
* Tells us which way to sort a column initially 
*/ 
private Map<Column<T, ?>, Boolean> defaultSortOrderMap = new HashMap<Column<T, ?>, Boolean>(); 
/** 
* Comparators associated with their columns 
*/ 
private Map<Column<T, ?>, Comparator<T>> comparators = new HashMap<Column<T, ?>, Comparator<T>>(); 
/** 
* Column to sort when the data provider's list is refreshed using 
* {@link SortedCellTable#setList(List)} 
*/ 
private Column<T, ?> initialSortColumn; 
/** 
* Data provider we will attach to this table 
*/ 
private ListDataProvider<T> dataProvider; 
/** 
* Special column sorting handler that will allow us to do more controlled 
* sorting 
*/ 
ListHandler<T> columnSortHandler; 

public SortedCellTable() { 
    super(); 
    dataProvider = new ListDataProvider<T>(); 
    dataProvider.addDataDisplay(this); 
    columnSortHandler = new ListHandler<T>(dataProvider.getList()) { 

     @Override 
     public void onColumnSort(ColumnSortEvent event) { 
      @SuppressWarnings("unchecked") 
      Column<T, ?> column = (Column<T, ?>) event.getColumn(); 
      if (column == null) { 
       return; 
      } 

      if (column.equals(currentlySortedColumn)) { 
       // Default behavior 
       super.onColumnSort(event); 
      } else { 
       // Initial sort; look up which direction we need 
       final Comparator<T> comparator = comparators.get(column); 
       if (comparator == null) { 
        return; 
       } 

       Boolean ascending = defaultSortOrderMap.get(column); 
       if (ascending == null || ascending) { 
        // Default behavior 
        super.onColumnSort(event); 
       } else { 
        // Sort the column descending 
        Collections.sort(getList(), new Comparator<T>() { 
         public int compare(T o1, T o2) { 
          return -comparator.compare(o1, o2); 
         } 
        }); 
        // Set the proper arrow in the header 
        getColumnSortList().push(
          new ColumnSortInfo(column, false)); 
       } 
       currentlySortedColumn = column; 
      } 
     } 

     @Override 
     public void setComparator(Column<T, ?> column, 
       Comparator<T> comparator) { 
      comparators.put(column, comparator); 
      super.setComparator(column, comparator); 
     } 

    }; 
    addColumnSortHandler(columnSortHandler); 
} 

/** 
* Adds a column to the table and sets its sortable state 
* 
* @param column 
* @param headerName 
* @param sortable 
*/ 
public void addColumn(Column<T, ?> column, String headerName, 
     boolean sortable) { 
    addColumn(column, headerName); 
    column.setSortable(sortable); 
    if (sortable) { 
     defaultSortOrderMap.put(column, true); 
    } 
} 

/** 
* Adds a column to the table and sets its sortable state 
* 
* @param column 
* @param header 
* @param sortable 
*/ 
public void addColumn(Column<T, ?> column, Header<?> header, 
     boolean sortable) { 
    addColumn(column, header); 
    column.setSortable(sortable); 
    if (sortable) { 
     defaultSortOrderMap.put(column, true); 
    } 
} 

/** 
* Sets the column to sort when the data list is reset using 
* {@link SortedCellTable#setList(List)} 
* 
* @param column 
*/ 
public void setInitialSortColumn(Column<T, ?> column) { 
    initialSortColumn = column; 
} 

/** 
* Sets a comparator to use when sorting the given column 
* 
* @param column 
* @param comparator 
*/ 
public void setComparator(Column<T, ?> column, Comparator<T> comparator) { 
    columnSortHandler.setComparator(column, comparator); 
} 

/** 
* Sets the sort order to use when this column is clicked and it was not 
* previously sorted 
* 
* @param column 
* @param ascending 
*/ 
public void setDefaultSortOrder(Column<T, ?> column, boolean ascending) { 
    defaultSortOrderMap.put(column, ascending); 
} 

/** 
* Sets the table's data provider list and sorts the table based on the 
* column given in {@link SortedCellTable#setInitialSortColumn(Column)} 
* 
* @param list 
*/ 
public void setList(List<T> list) { 
    dataProvider.getList().clear(); 
    if (list != null) { 
     for (T t : list) { 
      dataProvider.getList().add(t); 
     } 
    } 

    // Do a first-time sort based on which column was set in 
    // setInitialSortColumn() 
    if (initialSortColumn != null) { 
     Collections.sort(dataProvider.getList(), new Comparator<T>() { 

      @Override 
      public int compare(T o1, T o2) { 
       return (defaultSortOrderMap.get(initialSortColumn) ? 1 : -1) 
         * comparators.get(initialSortColumn) 
           .compare(o1, o2); 
      } 

     }); 
     // Might as well get the little arrow on the header to make it 
     // official 
     getColumnSortList().push(
       new ColumnSortInfo(initialSortColumn, defaultSortOrderMap 
         .get(initialSortColumn))); 
     currentlySortedColumn = initialSortColumn; 
    } 
} 
} 
1

zamiast

someTable.getColumnSortList().push(provider.getDefaultSortColumn()); 

użycie

someTable.getColumnSortList().push(new ColumnSortInfo(provider.getDefaultSortColumn(), false)); 
Powiązane problemy