2012-02-29 27 views
7

Tôi có một ô HTMLT có các cột có thể sắp xếp rất giống với ví dụ hướng dẫn của nhà phát triển (http://code.google.com/webtoolkit/doc/latest/DevGuideUiCellTable.html#columnSorting).Đặt thứ tự sắp xếp mặc định cho nhấp chuột đầu trang ban đầu trên bảng di động gwt

Tuy nhiên, tôi muốn có một số cột sắp xếp giảm dần thay vì tăng dần theo mặc định. Tức là, nếu cột A hiện không được sắp xếp và tôi nhấp vào tiêu đề của nó, tôi muốn cột A được sắp xếp giảm dần trên nhấp chuột đầu tiên đó và tăng dần lên lần nhấp thứ hai. Nhưng tôi vẫn muốn các cột khác sắp xếp theo thứ tự hiện tại - sử dụng thứ tự tăng dần trên nhấp chuột đầu tiên.

Ngoài phương thức Column.setSortable (boolean) để đặt khả năng sắp xếp và phương thức ColumnSortList.push (ColumnSortInfo) để kích hoạt sắp xếp theo cách thủ công, dường như không có nhiều quyền kiểm soát quá trình.

Có cách nào để thực hiện mục tiêu này không?

Trả lời

7

Column.setDefaultSortAscending(false) là cách dễ dàng để làm điều này; không yêu cầu CellTable tùy chỉnh.

+0

Có vẻ như điều này vừa được thêm vào api. Cảm ơn các cập nhật. – ahaurw01

+0

Được thêm vào gwt 2.5 – Helpa

2

Hãy xem GWT CellTable columnsorting. (Người nào đó đã bỏ phiếu vì họ không đồng ý với cách tiếp cận của tôi?)

Nó không phải là câu trả lời cho câu hỏi của bạn, nhưng nó ủng hộ bạn hiểu những điều cơ bản của Celltable trước khi thử bất cứ điều gì được minh họa bởi các ví dụ celltable.

Bằng cách hiểu được hành vi và yêu cầu cơ bản của các bảng Cell/Grid, việc sắp xếp các cột sẽ tự biểu hiện rõ ràng.

Việc sắp xếp được thực hiện bằng cách cung cấp một trình so sánh. Về phần tôi, tôi đã thử một vài cách để thực hiện so sánh để phù hợp với nhiều cách khác nhau mà tôi muốn các cột được sắp xếp.

+0

Cảm ơn đã phản ứng. Tôi đồng ý với bạn trong bài viết được liên kết của bạn rằng các ví dụ không truyền đạt đầy đủ các khả năng và cách tiếp cận cần thiết để tiếp tục với CellTable. Tôi đã có thể phân lớp CellTable để làm những gì tôi muốn.Tôi sẽ đăng bài như một phản ứng. – ahaurw01

+0

Để hỗ trợ những người đang tìm kiếm câu trả lời, bạn nên đánh dấu câu trả lời của mình là câu trả lời. –

+0

Cảm ơn bạn đã biết mẹo. Vì tôi mới, tôi muốn tôi chờ một ngày trước khi tôi có thể đánh dấu nó. – ahaurw01

5

Tôi đã có thể mở rộng CellTable để thực hiện chính xác những gì tôi muốn về cơ bản bằng cách ghi đè phương pháp onColumnSort(ColumnSortEvent) của ListHandler được liên kết với bảng. Đây là thịt/khoai tây của việc thực hiện. Tôi đã phải làm một chút bảo trì dưới nắp để theo dõi có hay không một cột đã được sắp xếp khi bạn sắp sắp xếp lại nó.

Một điều khiến tôi bối rối (và không rõ ràng trong ví dụ của google) là sử dụng phương pháp ColumnSortList.push() không thực sự kích hoạt một loại như nhấp, nhưng thay vào đó chỉ thay đổi trạng thái cơ bản về cách cột nghĩ được sắp xếp.

Khi tôi đã sẵn sàng để sử dụng bảng này tôi đã làm, tôi về cơ bản đã làm như sau:

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

Đây là việc thực hiện 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

thay vì

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

sử dụng

someTable.getColumnSortList().push(new ColumnSortInfo(provider.getDefaultSortColumn(), false)); 
Các vấn đề liên quan