dojo datagrid自定义排序服务器端

时间:2010-01-24 19:59:46

标签: sorting datagrid dojo header server-side

我正在使用dojo.data.ItemFileWriteStore绘制一个dojo数据网格(工作正常)并且网格显示正确。我正在使用客户端排序,这也很好。 但现在我需要改变排序并做那个服务器端。为此,我试图使用onHeaderCellClick事件,使用它我可以运行一个JavaScript函数..

之类的东西
 gridInfo = {
            store: myJsonStore,
            structure: myStructure
            onHeaderCellClick:getSortedTable
         };

现在这里是getSortedTable函数,我想用它来再次调用服务器 - 传递单元名,表名和排序顺序(asc或desc)。

 function getSortedTable(e)
    {
  var cellName = e.cell.name;
            var tableName = ?
            var sortOrder = ?
          // getSortedTablefromServer(cellName, sortOrder, tablename)
    }

但我能从“e”参数中脱离出来的唯一一个是单元格名称,可能是表格名称。

  1. 如何获取或保持天气跟踪,它将是用户所需的升序或降序。
  2. 此外 - 如何在列标题上显示小箭头以向用户显示数据是降序还是升序?
  3. 非常感谢任何帮助!!

    谢谢,

2 个答案:

答案 0 :(得分:0)

您可能最好修改数据存储以处理服务器端排序而不是网格(例如,将排序条件作为查询参数附加到fetch()触发的XHR调用,而不是排序结果集客户端)。网格应该与客户端排序完全一样,因为它只反映数据存储的排序顺序。

关于dojo.data.api.Read来自fetch()

If a sort parameter is specified, this is a indication to the datastore to 
sort the items in some manner before returning the items.  The array is an array of 
javascript objects that must conform to the following format to be applied to the
fetching of items:
    {
        attribute: attribute || attribute-name-string,
        descending: true|false;   // Optional.  Default is false.
    }
Note that when comparing attributes, if an item contains no value for the attribute
(undefined), then it the default ascending sort logic should push it to the bottom 
of the list.  In the descending order case, it such items should appear at the top of the list.

答案 1 :(得分:0)

我想你可能会改变你的商店。我正在使用带有jsonRestStore的网格。因此,每次通过单击列更改排序时,网格都会启动请求。

这是我的网格

require([
    "dojox/grid/EnhancedGrid",
    "dojox/data/JsonRestStore",
    "dojox/grid/enhanced/plugins/NestedSorting",
    "dojo/domReady!",
    "dojox/grid/cells/dijit"
], function(DataGrid, JsonRestStore) {
    dataStore = new JsonRestStore({
        target: "/url_to_your_be"
    });
    grid = new DataGrid({
        store: dataStore,
        plugins: {"nestedSorting": true},
        query: "?something=1",
        structure: [
            {
                defaultCell: { editable: false},
                cells: [
                    { name: "col 1", field: "col_1", width: "50px"},
                    { name: "col 2", field: "col_2", width: "50px"}
                ]
            }
        ],
        selectionMode: "single",
        sortFields: [{attribute: 'col_1', descending: false},{attribute: 'col_2', descending: false}]
    }, "yourGridId");
    grid.startup();
});

sortFields用于设置第一次加载的排序,如果您不需要,可以忽略它。

每次点击标题时,都会发送

之类的请求

http//www.yourwebsite.com/url_to_your_be/?something=1&sort(+col_1,+col_2)

即使您要更改查询

var grid = dijitRegistry.byId('yourGridId');
grid.setQuery("?something=2");

请求将是

`http//www.yourwebsite.com/url_to_your_be/?something=2&sort(+col_1,+col_2)`

现在您可以拆分BE中的$ _GET数据并进行排序

我的BE将数据作为json对象发送:

[{"col_1": 1, "col_2": "something"},...]

带有数据范围的标题:

Content-Range: items=0-10/100