gwt cell table dynamic columns - sorting

前端 未结 2 1918
死守一世寂寞
死守一世寂寞 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:57

    Here is the data grid code

    indexedColumn.setSortable(true);
    sortHandler.setComparator((Column) indexedColumn, (Comparator) indexedColumn.getComparator(true));
    

    Here is the actual class

        public class IndexedColumn extends Column, String>
    {
       private Comparator> forwardComparator;
       private Comparator> reverseComparator;
       private final int index;
    
       public IndexedColumn(int index)
       {
          super(new TextCell());
          this.index = index;
       }
    
       @Override
       public String getValue(List object)
       {
          return object.get(index);
       }
    
       public Comparator> getComparator(final boolean reverse)
       {
          if (!reverse && forwardComparator != null)
          {
             return forwardComparator;
          }
          if (reverse && reverseComparator != null)
          {
             return reverseComparator;
          }
          Comparator> comparator = new Comparator>()
          {
             public int compare(List o1, List 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;
       }
    
    }
    

提交回复
热议问题