如何刷新数据表格

8

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

6个回答

22
为了改变网格中的值,您需要更改网格存储中的值。网格数据绑定到存储数据上,网格会根据需要自行更新。
因此,关键是要理解Dojo的数据API以及存储在Dojo中的工作方式。不要直接在网格中操作数据,而是在存储中操作数据。
理想情况下,存储应该是您在应用程序运行时操纵的数组,并且您不需要将数组与网格同步。除非不可能,否则只需使用ItemFileWriteStore作为数据持有者即可。
此外,如果可能,使用Dojo数据身份API可以使在网格中查找项目变得更加简单。假设您知道何时在应用程序中更新、删除或更改项目,则应该能够在发生操作时根据需要修改网格存储。这绝对是首选方法。如果无法执行此操作,则必须进行一般获取并使用onComplete回调手动同步数组,这将非常缓慢且不具备可扩展性,在这种情况下,您可能需要创建一个全新的存储并将其分配给网格grid.setStore(myNewStore)。
这里有一个带有基本创建、更新和删除操作的fiddle示例:http://jsfiddle.net/BC7yT/11/ 这些示例都利用了在创建存储时声明标识(identity)的功能。
var store = new dojo.data.ItemFileWriteStore({
    data: {
        identifier : 'planet',
        items: itemList
    }
});

更新现有项目:

//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数据表格参考指南(添加/删除行示例,更新表格数据示例)


3

我想你已经有一个可用的网格,并且想要完全更改网格的存储。

  1. Create a new datastore with your new value :

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

    (data is the reponse from an ajax request for me)

  2. Change your grid's store with the new one :

    grid.store = dataStore;
    
  3. Render :

    grid.render();
    

3

这将在最新版本的Dojo 1.9中更新网格存储并刷新网格视图。

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

1
我有一个服务器端过滤的EnhancedGrid,通过更改存储器进行刷新,并在其他答案中显示。
然而,当应用筛选器时,我有另一个EnhancedGrid无法刷新。可能与客户端过滤有关(但数据仍来自使用JsonRest存储器的服务器),但我不知道原因。无论如何,解决方案是使用以下代码进行刷新:
grid.setFilter(grid.getFilter());

如果其他方法都失败了,这个方法虽然有些不正规和奇怪,但也许能解决问题。

0

使用这个,我可以更新指定的行。这个例子是针对一个树形表格的。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接