gwt cell table dynamic columns - sorting

前端 未结 2 1886
死守一世寂寞
死守一世寂寞 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条回答
  • 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<List<String>, String> {
    
        private final int index;
    
        public IndexedColumn(int index) {
            super(new EditTextCell());
            this.index = index;
        }
    
        @Override
        public String getValue(List<String> object) {
            return object.get(this.index);
        }
    
        public int getIndex(){
            return index;
        }
    
    }
    

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

    ListHandler<List<String>> columnSortHandler = new ListHandler<List<String>>(list);
        columnSortHandler.setComparator(columnName, new Comparator<List<String>>() {
    
            public int compare(List<String> o1, List<String> 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<List<String>> 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.

    0 讨论(0)
  • 2021-01-25 07:57

    Here is the data grid code

    indexedColumn.setSortable(true);
    sortHandler.setComparator((Column<T, ?>) indexedColumn, (Comparator<T>) indexedColumn.getComparator(true));
    

    Here is the actual class

        public class IndexedColumn extends Column<List<String>, String>
    {
       private Comparator<List<String>> forwardComparator;
       private Comparator<List<String>> reverseComparator;
       private final int index;
    
       public IndexedColumn(int index)
       {
          super(new TextCell());
          this.index = index;
       }
    
       @Override
       public String getValue(List<String> object)
       {
          return object.get(index);
       }
    
       public Comparator<List<String>> getComparator(final boolean reverse)
       {
          if (!reverse && forwardComparator != null)
          {
             return forwardComparator;
          }
          if (reverse && reverseComparator != null)
          {
             return reverseComparator;
          }
          Comparator<List<String>> comparator = new Comparator<List<String>>()
          {
             public int compare(List<String> o1, List<String> o2)
             {
                if (o1 == null && o2 == null)
                {
                   return 0;
                }
                else if (o1 == null)
                {
                   return reverse ? 1 : -1;
                }
                else if (o2 == null)
                {
                   return reverse ? -1 : 1;
                }
    
                // Compare the column value.
                String c1 = getValue(o1);
                String c2 = getValue(o2);
                if (c1 == null && c2 == null)
                {
                   return 0;
                }
                else if (c1 == null)
                {
                   return reverse ? 1 : -1;
                }
                else if (c2 == null)
                {
                   return reverse ? -1 : 1;
                }
                int comparison = ((String) c1).compareTo(c2);
                return reverse ? -comparison : comparison;
             }
          };
    
          if (reverse)
          {
             reverseComparator = comparator;
          }
          else
          {
             forwardComparator = comparator;
          }
          return comparator;
       }
    
    }
    
    0 讨论(0)
提交回复
热议问题