在服务器端对 CellTable 进行排序

发布于 2024-12-02 09:22:59 字数 979 浏览 6 评论 0原文

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

现在好了! :-)

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

异步远程排序部分很好地展示了如何对我的 AsyncDataProvider 进行排序,但是...我如何检索用户想要排序的列的名称

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

但是如何从中获取字符串名称呢?我只是想知道“surname”或“soldDate”,列绑定的字段的名称!然后我将它传递给我的 rpc 服务,并使用它对服务器端数据进行排序 query(...).order()

我是否遗漏了什么?

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

I'm currently using a Gwt CellTable, bound to my GAE/Objectify backend via RPC calls.

All right now! :-)

Then I want to sort columns, so I read http://code.google.com/intl/it-IT/webtoolkit/doc/latest/DevGuideUiCellTable.html#columnSorting

The Async Remote sorting sections shows very well how to get sorting into my AsyncDataProvider but... how can I retrieve the name of the column the user wants to sort?

It shows this code: ColumnSortList sortList = table.getColumnSortList();

But how can I get String names from that? I simply want to know "surname" or "soldDate", the name of the field the column is bound to! Then I will pass it to my rpc service, and use it to sort data server-side query(...).order(<field_name>)

Am I missing something?

UPD: interesting stuff here: http://groups.google.com/group/google-web-toolkit/browse_thread/thread/77a0eaf8086218a6/effb8d3abe69270b#effb8d3abe69270b

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听风念你 2024-12-09 09:22:59

您可以按照表中的方式保留列名称的列表:

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 是单元格表中显示的数据对象。

You can keep a list of column names ordered as they are in the table:

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

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

// add the other columns

Then when you need to get the sort column name:

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

*where MyEntity is your data object displayed in the cell table.

避讳 2024-12-09 09:22:59

聚会有点晚了,但这里有一个基于 当前文档(请参阅“使用 AsyncDataProvider 进行列排序”部分)。

当我们添加列时,我们可以简单地设置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/客户端使用的列名称不同的名称(例如数据库列名称)。

A bit late to the party, but here's a more straight-forward solution based off of the current documentation (see section 'ColumnSorting with AsyncDataProvider').

When we're adding our columns we can simply set the 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

Then we can retrieve the column's dataStoreName later when sorting:

@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>>() {...}
    );
    ...
}

Using this method we can pass the column name directly to our RPC method. It even allows us to use a different name (eg. the database column name) than the column name used on the UI/client side.

舂唻埖巳落 2024-12-09 09:22:59

我已经使用类似的东西作为应用程序列对象。

公共类 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();

I have used something like this as an application column object.

public class 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;

}

Now create a HashMap of the above as follows:

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

Add all the columns as you create them both in the ScrollTableColumn and in the columnMap.

Finally you can get the required name as:

ColumnSortList sortList = dataTable.getColumnSortList();
Column<?, ?> column = sortList.get(0).getColumn();
ColumnNameEnum = columnMap.get(column);
String name = ColumnNameEnum.getName();
南七夏 2024-12-09 09:22:59

正确的方法是扩展基列类,这将允许您覆盖单元格渲染,通过构造函数传入列配置,最重要的是设置 DataStoreName,这是您应该存储列的字段名称的位置。最后,您不应该重用 onrangechanged 火灾,而应该通过覆盖它来直接访问列排序处理程序。范围更改和列排序处理程序应该调用某种类型的方法,您必须更新网格。为了理智起见,我将我的称为 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 请求的方法。您应该将此信息存储在数据网格类的私有成员中,而不是记录日志,以便您的请求可以参数化它们。

您也可以使其适用于本地缓存,只需在本地从服务器复制数据,然后返回该缓存集合的排序集合,而不是调用 updateDataList 方法。

现在,您不需要只为字符串名称存储单独的列表,这会浪费内存,更不用说如果列顺序因用户交互或其他原因而更改,还会出现同步性问题。

The proper way is to extend the base column class which will allow you to override cell rendering, pass in column configuration via your constructor, and most importantly set the DataStoreName which is where you should store the field name for the column. Lastly you should not reuse the onrangechanged fire, but access the columnsort handler directly by overriding it. on range change and column sort handler should call some type of method that you have to update your grid. I call mine updateGrid for sanity. This allows you to set any grid parameters used by your async request to specific sort column and direction. The main reason you want to use column sort handler is to access the ColumnSort event which contains your sort direction information

your column class that extends the base GWT column. You can also extend date or number columns too.

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

create your handler

dataGrid.addColumnSortHandler(new DataGridSortEvent());

your sort event class

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 is your method you use to make the actual AJAX request to your server side. rather then logging you sould store this info in private members of your datagrid class so that your request can parameterize them.

you could also make this work for local caching too, just make a copy of the data from your server locally then return a sorted collection of that cached collection, rather then calling the updateDataList method.

Now you do not need to store a separate list for just string names, which is waste of memory not to mention a synchronicity issue if the column order is change from user interaction or whatever.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文