Question

You can represent your "rows" as List<String> instances, you have to change your parameterization from String to List in your Grid, Column and data provider; and of course you have to call updateRowData with a List<List<String>>, not a List<String>.

You also need one Column instance per column, taking the value out of the List by 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);
       }

   }

How do i add sorting to this example. I tried a ListHandler but not sure how to compare List<String>. Any help is appreciated.

Was it helpful?

Solution 2

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

}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top