2011-03-31 67 views

回答

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

UPDATE AN 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); 

       } 
     }); 

插入一个新的项:

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

删除项目:

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

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

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

编辑:

Dogo data grid reference guide(添加/删除行例如,更新的网格数据的例子)

0

与此我可以更新specifi行。这个例子是针对treegrid的。

var idx = this.treeGrid.getItemIndex(item); 
            if(typeof idx == "string"){ 
             this.treeGrid.updateRow(idx.split('/')[0]); 
            }else if(idx > -1){ 
             this.treeGrid.updateRow(idx); 
            } 
3

(我想你已经有一个工作网格,要彻底改变网格的商店)

  1. 与你的新值创建一个新的数据存储:

    dataStore = new ObjectStore({ objectStore:new Memory({ data: data.items }) }); 
    

    (数据是来自ajax请求的响应)

  2. 更改网格的s用新的非限制性定语从句:

    grid.store = dataStore; 
    
  3. 渲染:

    grid.render(); 
    
2

这将更新网格存储和刷新最新网格的查看版本道场1.9

grid.store = store; 
grid._refresh(); 
1

我有一个服务器端过滤的EnhancedGrid,它通过更改商店而愉快地刷新,并在其他答案中显示。

但是我有另一个EnhancedGrid,当应用过滤器时不会刷新。这可能与它过滤客户端的事实有关(但数据仍然来自服务器使用JsonRest存储),但我不知道原因。 Eitherway,该解决方案是用下面的代码刷新:

grid.setFilter(grid.getFilter()); 

这是哈克和陌生,但如果一切都失败了......