且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

dojo datagrid自定义排序服务器端

更新时间:2023-12-04 10:31:04

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



这是我的网格

  require([
dojox / grid / EnhancedGrid,
dojox / data / JsonRestStore,
dojox / grid / enhanced /插件/ 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:[
{名称:col 1,字段:col_1,width:50px},
{name:col 2,field:col_2,width:50px}
]
}
],
selectionMode:singl e,
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},...] 

与数据范围的标题:

 内容范围:items = 0-10 / 100 


I am using dojo.data.ItemFileWriteStore to draw a dojo datagrid (which works fine) and the grid shows properly. I was using client side sorting and that was also working fine. but now I need to change the sorting and do that server side. For this I am trying to use onHeaderCellClick event, using which I am able to run a javascript function.. something like

 gridInfo = {
            store: myJsonStore,
            structure: myStructure
            onHeaderCellClick:getSortedTable
         };

Now here is the getSortedTable function which I want to use to make another call to the server - passing the cell name, Table Name and the sort Order (asc or desc).

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

but the only thihng I am able to get out of from the 'e' parameter is the cell Name and may be the table Name.

  1. How can I get or keep a track of weather it will be ascending order required by the user or is it descending order.
  2. Also - how will I show the little arrow on the header of the column to show the user that the data is in descending or ascending?

Any help is highly appreciated!!

Thanks,

I think you may change your store. I'm using the grid with the jsonRestStore instead. So the grid is starting a request every time I change the sorting by clicking on a column.

Here is my grid

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 is for setting the sort on your first load and can be ignored if you don't need it.

Every time you click in the header it is sending a request like

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

Even if you are changing the query

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

the request will be

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

Now you can split the $_GET data in your BE and do the sort

My BE is sending the data as json object:

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

with the header of data range:

Content-Range: items=0-10/100