对CellTable服务器端进行排序

时间:2011-09-01 15:07:36

标签: google-app-engine gwt cell objectify

我目前正在使用Gwt CellTable,通过RPC调用绑定到我的GAE / Objectify后端。

现在好了! : - )

然后我想对列进行排序,所以我读了http://code.google.com/intl/it-IT/webtoolkit/doc/latest/DevGuideUiCellTable.html#columnSorting

Async Remote排序部分非常清楚地展示了如何排序到我的AsyncDataProvider但是...如何检索用户想要排序的

它显示了以下代码:ColumnSortList sortList = table.getColumnSortList();

但我如何从中获取字符串名称?我只想知道“姓氏”或“soldDate”,列所绑定的字段的名称!然后我将它传递给我的rpc服务,并用它来排序数据服务器端query(...).order(<field_name>)

我错过了什么吗?

UPD :这里有趣的内容:http://groups.google.com/group/google-web-toolkit/browse_thread/thread/77a0eaf8086218a6/effb8d3abe69270b#effb8d3abe69270b

4 个答案:

答案 0 :(得分:5)

您可以保留列表中列出的列名列表:

List<String> columnNames = new ArrayList<String>();

table.addColumn(surnameColumn, "surname");
columnNames.add("surname");

// add the other columns

然后当您需要获取排序列名称时:

String sortColumnName;
ColumnSortList sortList = table.getColumnSortList();
if (sortList != null && sortList.size() != 0){
     Column <MyEntity, ?> sortColumn = (Column <MyEntity, ?>) 
                                           sortList.get(0).getColumn();
     Integer columnIndex = table.getColumnIndex(sortColumn);
     sortColumnName = columnNames.get(columnIndex);
}

// do your rpc call

*其中MyEntity是您在单元格表中显示的数据对象。

答案 1 :(得分:1)

聚会晚了一点,但这是基于current documentation的更直接的解决方案(请参阅“使用AsyncDataProvider进行ColumnSorting”一节)。

当我们添加列时,我们可以简单地设置dataStoreName

TextColumn<MyData> surname = new TextColumn<MyData>() {
    ...
}
surname.setSortable(true);
surname.setDataStoreName("surname");  // Set the column name
table.getColumnSortList().push(surname);
table.addColumn(surname, "Last Name");  // eg. A different name for the UI

然后我们可以在排序时稍后检索列的dataStoreName

@Override
protected void onRangedChanged(HasData<MyData> display) {
    ...
    ColumnSortList.ColumnSortInfo info = table.getColumnSortList().get(0);
    String sortColumn = info.getColumn().getDataStoreName();  // Get the column name
    boolean sortIsAscending = info.isAscending();

    rpcService.requestMyData(
        sortColumn,
        sortIsAscending,
        new AsyncCallback<ArrayList<MyData>>() {...}
    );
    ...
}

使用此方法,我们可以将列名直接传递给我们的RPC方法。它甚至允许我们使用与UI /客户端上使用的列名称不同的名称(例如,数据库列名称)。

答案 2 :(得分:0)

我使用过像这样的东西作为应用程序列对象。

公共类ScrollTableColumn    {

    // --------------------------------------------------------------- Field(s)

    private int            sequence;
    private Column         column;
    private Header         header;
    private int            size;
    private int            calculatedSize;
    private boolean        show;
    private PartialColumn  partialColumn;
    private ColumnNameEnum columnName;

}

现在按如下方式创建上面的HashMap:

Map<Column, ScrollTableColumn> columnMap 
    = new HashMap<Column, ScrollTableColumn>();

在ScrollTableColumn和columnMap中创建它们时添加所有列。

最后,您可以获得所需的名称:

ColumnSortList sortList = dataTable.getColumnSortList();
Column<?, ?> column = sortList.get(0).getColumn();
ColumnNameEnum = columnMap.get(column);
String name = ColumnNameEnum.getName();

答案 3 :(得分:0)

正确的方法是扩展基列类,这将允许您覆盖单元格渲染,通过构造函数传递列配置,最重要的是设置DataStoreName,您应该存储列的字段名称。最后,您不应该重用onrangechanged fire,而是直接通过覆盖它来访问columnsort处理程序。对范围更改和列排序处理程序应调用某种类型的方法来更新网格。我称我的updateGrid为理智。这允许您将异步请求使用的任何网格参数设置为特定的排序列和方向。要使用列排序处理程序的主要原因是访问包含排序方向信息的ColumnSort事件

扩展基本GWT列的列类。您也可以扩展日期或数字列。

public GridStringColumn(String fieldName, String text, String tooltip, boolean defaultShown, boolean sortable, boolean hidden) {
        super(new TextCell());
        setDataStoreName(fieldName);
        this.text_ = text;
        this.tooltip_ = tooltip;
        this.defaultShown_ = defaultShown;
        setSortable(sortable);
        this.hidden_ = hidden;
    }

创建处理程序

dataGrid.addColumnSortHandler(new DataGridSortEvent());

您的排序事件类

protected class DataGridSortEvent implements ColumnSortEvent.Handler {

        @Override
        public void onColumnSort(ColumnSortEvent event) {
            ColumnSortList sortList = dataGrid_.getColumnSortList();
            if (sortList != null && sortList.size() > 0) {
                Column<T, ?> sortColumn = (Column<T, ?>) sortList.get(0).getColumn();
                LOG.info("col_sorta: " + event.isSortAscending());
                LOG.info("col_index: " + sortColumn.getDataStoreName());
                updateDataList();
            }
        }
    }

updateDataList是您用来向服务器端发出实际AJAX请求的方法。而不是记录您应该将此信息存储在您的datagrid类的私有成员中,以便您的请求可以参数化它们。

你也可以使这个工作用于本地缓存,只需从服务器本地复制数据,然后返回该缓存集合的有序集合,而不是调用updateDataList方法。

现在您不需要为字符串名称存储单独的列表,这会浪费内存,更不用说如果列顺序从用户交互或其他任何内容发生更改时会出现同步问题。

相关问题