Adding rows with sorting in CellTable

∥☆過路亽.° 提交于 2019-12-20 05:51:40

问题


I want to sort rows in CellTable when adding new.

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

And added a few columns:

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

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.

How can I do it?


回答1:


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

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



来源:https://stackoverflow.com/questions/19257216/adding-rows-with-sorting-in-celltable

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