gwt cell table dynamic columns - sorting

老子叫甜甜 提交于 2019-12-02 04:38:19

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.

user2130039

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

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!