在CellTable中添加排序行 [英] Adding rows with sorting in CellTable

查看:110
本文介绍了在CellTable中添加排序行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在添加新内容时对CellTable中的行进行排序。

I want to sort rows in CellTable when adding new.

要标记UI我使用UIBinder:

To markup UI I use UIBinder:

<g:HTMLPanel>
<c:CellTable pageSize='100' ui:field='myTable'/>
<c:SimplePager ui:field='myPager' location='CENTER'/>
</g:HTMLPanel>

在小部件中我创建了一个表和分页:

In the widget I created a table and pagination:

@UiField(provided=true) CellTable<myDTO> myTable;
SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
myPager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
myTable = new CellTable<myDTO>();

然后我安装了一个选择模型:

Then I installed a selection model:

final NoSelectionModel<myDTO> selectionModel = new NoSelectionModel<myDTO>();
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
   public void onSelectionChange(SelectionChangeEvent event) {
      clickedObject = selectionModel.getLastSelectedObject();
   }
});     

myTable.setPageSize(50);
myTable.setSelectionModel(selectionModel);

并添加了几列:

Column<myDTO, String> column1 = new Column<myDTO, String>(new TextCell()) {
   @Override
   public String getValue(myDTO data) {
      return data.getSomeData1();
   }
};

Column<myDTO, String> column2 = new Column<myDTO, String>(new TextCell()) {
   @Override
   public String getValue(myDTO data) {
      return data.getSomeData2();
   }
};
...
Column<myDTO, String> columnN = new Column<myDTO, String>(new TextCell()) {
   @Override
   public String getValue(myDTO data) {
      return data.getSomeDataN();
   }
};      

myTable.addColumn(column1, "name of column1");
myTable.addColumn(column2, "name of column2");
...
myTable.addColumn(columnN, "name of columnN");

接下来,我创建 AsyncDataProvider

Next, I create AsyncDataProvider:

AsyncDataProvider<myDTO> provider = new AsyncDataProvider<myDTO>() {
   @Override
   // is called when the table requests a new range of data 
   protected void onRangeChanged(HasData<myDTO> display) {

      final int start = display.getVisibleRange().getStart();
      final int lenght = display.getVisibleRange().getLength();

       myService.findAll(new AsyncCallback<List<myDTO>>() {
          public void onFailure(Throwable caught) {
             // exception handling here
          }

          public void onSuccess(List<myDTO> data) {
             updateRowCount(data.size(), true);
             updateRowData(0, data);
          }
      });
   }
 };

 provider.addDataDisplay(myTable);

如果我使用这种方法,那么新行将添加到表的末尾。

If I use this approach, then new rows are added to the end of the table.

我需要在添加时自动对行进行排序。

I need to automatically sort rows when added.

我该怎么做?

我将非常感谢这些信息。谢谢大家。

I would be very grateful for the information. Thanks to all.

推荐答案

在创建提供商后立即创建排序处理程序:

Create a sort handler right after creating your provider:

ListHandler<myDTO> sortHandler = new ListHandler<myDTO>(provider.getList());
myTable.addColumnSortHandler(sortHandler);

然后,对于要排序的每列,设置比较器并将列添加到排序列表,例如:

Then for each column that you want to sort by, set a comparator and add the column to the sort list, e.g.:

sortHandler.setComparator(column1, new Comparator<myDTO>() {
    public int compare(myDTO dto1, myDTO dto2) {
        // This is an example, how you compare them depends on the context
        return dto1.getSomeData1().compareTo(dto2.getSomeData1());
    }
});

myTable.getColumnSortList().push(column1);

您可以调用 push()方法多次按多列排序。您也可以为同一列调用它两次以反转其排序顺序(升序/降序)。

You can call the push() method multiple times to sort by multiple columns. You can also call it twice for the same column to invert its sorting order (ascending/descending).

这篇关于在CellTable中添加排序行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆