本文介绍了gwt单元格表动态列 - 排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以将行表示为列表< String> 实例,您必须将您的参数设置从字符串更改为网格,列和数据提供者中的列表;当然,您必须使用列表< String> 而不是列表< String> 。

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>.

您还需要每列一列Column实例,通过索引将值从列表中取出:

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

   }

我如何添加排序到这个例。我尝试了一个 ListHandler ,但不知道如何比较 List< String>

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

推荐答案

这是数据网格代码

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

这是实际的课程

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

}

这篇关于gwt单元格表动态列 - 排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:06
查看更多