如何刷新datagrid

Dam*_*mir 8 dojo dojox.grid dojox.grid.datagrid

我创建了dojox.grid.datagrid,我从数组中填充了内容,例如页面上的示例最后一个示例.在一段时间内,我在代码中更改该数组的值.如何刷新该网格的内容?如何从更改的数组加载新数据?

Tom*_*ner 22

要更改网格中的值,您需要更改网格存储中的值.网格数据绑定到商店数据,网格将根据需要自行更新.

所以关键是要了解Dojo的数据api以及商店如何在Dojo中运行.而不是直接在网格中操纵数据,而是在商店中操纵它.

理想情况下,存储是您在应用程序运行时操作的数组,您不需要将数组同步到网格.只需使用ItemFileWriteStore作为数据持有者,除非那是不可能的.

此外,如果可能的话,使用dojo数据标识api可以非常简单地在网格中查找项目.假设您知道应用程序中的项目何时更新,删除或更改,您应该能够在操作发生时根据需要修改网格存储.这绝对是首选方法.如果你不能这样做,你将不得不做一般的提取并使用onComplete回调来手动同步你的数组,这将非常慢,并且不能很好地扩展,在这种情况下你也可以只创建一个新的商店一起使用grid.setStore(myNewStore)将其分配给网格

这是一个基本的创建,更新和删除操作的小提琴:http://jsfiddle.net/BC7yT/11/

这些示例都利用在创建商店时声明身份.

var store = new dojo.data.ItemFileWriteStore({
    data: {
        identifier : 'planet',
        items: itemList
    }
});
Run Code Online (Sandbox Code Playgroud)

更新一个EXISITNG项目:

//If the store is not in your scope you can get it from the grid
var store = grid.store;
//fetchItemByIdentity would be faster here, but this uses query just to show 
//it is also possible
store.fetch({query : {planet : 'Zoron'},
             onItem : function (item ) {

                  var humans = store.getValue(item, 'humanPop');
                  humans += 200;
                  store.setValue(item, 'humanPop', humans);

              }
        });
Run Code Online (Sandbox Code Playgroud)

插入一个新项目:

store.newItem({planet: 'Endron', humanPop : 40000, alienPop : 9000});
               } catch (e) { 
                  //An item with the same identity already exists
               }
Run Code Online (Sandbox Code Playgroud)

删除项目:

store.fetchItemByIdentity({ 'identity' : 'Gaxula',  onItem :  function (item ) {
    if(item == null) {
         //Item does not exist
    } else {
        store.deleteItem(item);
    }
}});
Run Code Online (Sandbox Code Playgroud)


And*_*rei 7

以下代码段可用于更新网格:

var newStore = new dojo.data.ItemFileReadStore({data: {... some new data ...});
var grid = dijit.byId("gridId");
grid.setStore(newStore);
Run Code Online (Sandbox Code Playgroud)

编辑:

Dogo数据网格参考指南(添加/删除行示例,更新网格数据示例)