gwt cell table dynamic columns - sorting

前端 未结 2 1890
死守一世寂寞
死守一世寂寞 2021-01-25 07:17

You can represent your \"rows\" as List instances, you have to change your parameterization from String to List in your Grid, Column and data provider

2条回答
  •  闹比i
    闹比i (楼主)
    2021-01-25 07:52

    You need to add a ListHandler to each column you want to sort separately. Kind of like this:

    You'll have to add a getter method to IndexedColumn for the index:

    class IndexedColumn extends Column, String> {
    
        private final int index;
    
        public IndexedColumn(int index) {
            super(new EditTextCell());
            this.index = index;
        }
    
        @Override
        public String getValue(List object) {
            return object.get(this.index);
        }
    
        public int getIndex(){
            return index;
        }
    
    }
    

    Then you'll need to add a ListHandler to the CellTable:

    ListHandler> columnSortHandler = new ListHandler>(list);
        columnSortHandler.setComparator(columnName, new Comparator>() {
    
            public int compare(List o1, List o2) {
              if (o1 == o2) {
                return 0;
              }
    
              // Compare the column.
              if (o1 != null) {
                int index = columnName.getIndex();
                return (o2 != null) ? o1.get(index).compareTo(o2.get(index)) : 1;
              }
              return -1;
            }
        });
    table.addColumnSortHandler(columnSortHandler);
    

    In the example above list is the List> object. The columnName is the Column object. You'll have to do this for every column you want to sort.

    Don't forget to also call .setSortable(true) on each of the columns that you will sort.

    A good basic example of column sorting can be found here. The code above is based on this example but I used your index in IndexedColumn in order to get the proper String for the column to do the comparison.

提交回复
热议问题