gwt celltable中的列不排序

时间:2013-06-16 11:56:43

标签: sorting gwt celltable

我希望借助ListHandler为celltable中的列添加排序。但它没有排序。我不明白为什么它不起作用。我的代码基于GWT教程。 请给我一些建议。

Contract.java

public class Contract implements Serializable {
  private int contId;
  private String contOrgName;

  //getters and setters...   
}

Main.java

TextColumn<Contract> orgNameColumn = new TextColumn<Contract>() {
  @Override
  public String getValue(Contract contract) {
  return contract.getcontOrgName();
}};
orgNameColumn.setSortable(true);

CellTable<Contract> tableContract = new CellTable<Contract>();
tableContract.addColumn(orgNameColumn, "OrgName");
ListDataProvider<Contract> contractDataProvider = new ListDataProvider<Contract>();
contractDataProvider.addDataDisplay(tableContract);

GetContractsServiceAsync getContractsService = GWT.create(GetContractsService.class);
getContractsService.getContracts(new AsyncCallback<List<Contract>>() {
public void onFailure(Throwable caught) {
  // Show the RPC error message to the user
}
public void onSuccess(List<Contract> result) {
  contractDataProvider.getList().clear();
  contractDataProvider.getList().addAll(result);    
  ListHandler<Contract> columnSortHandler = new ListHandler<Contract>(result);
  columnSortHandler.setComparator(orgNameColumn, new Comparator<Contract>() {
    public int compare(Contract o1, Contract o2) {
    if (o1 == o2) {
    return 0;
    }
    if (o1 != null) {
    return (o2 != null) ? o1.getcontOrgName().compareTo(o2.getcontOrgName()) : 1;
    }
    return -1;
    }
});
tableContract.addColumnSortHandler(columnSortHandler);
table.getColumnSortList().push(orgNameColumn);
}
});

3 个答案:

答案 0 :(得分:4)

嘿,我也面临同样的问题。然后我知道对象引用ID与我添加到表中的不同。一旦我明白了,很容易找到解决方案。 另外,请在向ListHandler添加列之前注册CellTable

请注意评论的修改:

comparator没有任何问题。问题仍然存在。与CellTable相比,它反映了比较器中的对象错误。

请尝试更换以下代码:

contractDataProvider.getList().addAll(result);    
ListHandler<Contract> columnSortHandler = new ListHandler<Contract>(contractDataProvider.getList());

答案 1 :(得分:0)

尝试将columnSortHandler的创建移出onSuccess方法。在创建列后立即放置它。 在添加columnSortHandler之前添加结果的那一刻。我认为这是错误的顺序。

答案 2 :(得分:0)

我的情况,我按照这样排序:

private void sortColumn( List<CModel> list ) {
    int count = listOfColumns.size(); //upon adding the columns, i have created a list that will hold all the columns

    if ( list.size() <= 0 ) {

        for ( int i = 0; i < count; i++ ) {
            listOfColumns.get(i).setSortable( false );
        }

    } else {

        for ( int i = 0; i < count; i++ ) {
            sort( i );
            dg.redrawHeaders();
        }
    }
}       

private void sort(int i) {
    String[] listColHead = { /** list of column headers */ };

    Column<CModel,?> column = listCheckoutColumns.get( i );
    final String valueToCompare = listColHead[i];

    final ListDataProvider<CModel> dataProvider = new ListDataProvider<CModel>();

    // Connect the table to the data provider
    dataProvider.addDataDisplay( dg );

    // Add the data to the data provider, which automatically pushes it to the widget
    List<CModel> list = dataProvider.getList();
    for ( CModel product : listCheckout ) {
        list.add( product );
    }

    column.setSortable( true );

    // Add a ColumnSortEvent.ListHandler to connect sorting to the List
    ListHandler<CModel> columnSortHandler = new ListHandler<CModel>( list );
    columnSortHandler.setComparator( column, new Comparator<CModel>() {
        public int compare( CModel p1, CModel p2 ) {
            if ( p1 == p2 ) {
                return 0;
            }

            // Compare the columns.
            if ( p1 != null ) {
                if ( valueToCompare.equals( "code" ) ) {
                    return ( p2 != null ) ? p1.getFproductid().compareTo( p2.getFproductid() ) : 1;
                }

                else if ( valueToCompare.equals( "desc" ) ) {
                    return ( p2 != null ) ? p1.getFproduct_name().compareTo( p2.getFproduct_name() ) : 1;
                }

                else if ( valueToCompare.equals( "type" ) ) {
                    return ( p2 != null ) ? 
                            uec.computeTypeLabel( p1 ).compareTo( uec.computeTypeLabel( p2 ) ) : 1;
                }

                else if ( valueToCompare.equals( "maxex" ) ) {
                    return ( p2 != null ) ? uec.exQtyLabel( p1 ).compareTo( uec.exQtyLabel( p2 ) ) : 1;
                }

                else if ( valueToCompare.equals( "unit" ) ) {
                    return ( p2 != null ) ? p1.getFuom().compareTo( p2.getFuom() ) : 1;
                }

                else if ( valueToCompare.equals( "status" ) ) {
                    return ( p2 != null ) ? uec.qtyLabel( p1 ).compareTo( uec.qtyLabel( p2 ) ) : 1;
                }

                else if ( valueToCompare.equals( "qty" ) ) {
                    return ( p2 != null ) ? Double.compare( p1.getFqty(), p2.getFqty() ) : 1;
                }

                else if ( valueToCompare.equals( "price" ) ) {
                    return ( p2 != null ) ? uec.extPriceLabel( p1 ).compareTo( uec.extPriceLabel( p2 ) ) : 1;
                }

                else if ( valueToCompare.equals( "total" ) ) {
                    return ( p2 != null ) ? Double.compare( p1.getFtotal_line(), p2.getFtotal_line() ) : 1;
                }
            }
            return -1;
        }
    });

    dg.addColumnSortHandler( columnSortHandler );
    dg.getColumnSortList().push( column );
}

每次,我都会为我的DataGrid列表设置数据,

    dgCheckout.redraw();

    dgCheckout.setRowCount( lengthOfList, true );
    dgCheckout.setRowData( 0, newList );

    sortColumn( newList );