【问题标题】:How to refresh datagrid如何刷新数据网格
【发布时间】:2011-07-26 20:20:18
【问题描述】:

我创建 dojox.grid.datagrid 并从数组中填充内容,例如 last example on page。在一段时间内,我在代码中更改了该数组的值。如何刷新该网格的内容?如何从改变的数组中加载新数据?

【问题讨论】:

    标签: dojo dojox.grid dojox.grid.datagrid


    【解决方案1】:

    要更改网格中的值,您需要更改网格存储中的值。网格数据与存储数据绑定,网格会根据需要自行更新。

    所以关键是要了解Dojo的数据api以及Dojo中store是如何工作的。与其直接在网格中操作数据,不如在存储中操作。

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

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

    这里有一个基本的创建、更新和删除操作:http://jsfiddle.net/BC7yT/11/

    这些示例都利用了在创建商店时声明身份的优势。

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

    【讨论】:

      【解决方案2】:

      以下代码sn -p可用于更新网格:

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

      编辑:

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

      【讨论】:

        【解决方案3】:

        (我想你已经有一个工作网格并且你想完全改变网格的存储)

        1. 使用您的新值创建一个新数据存储:

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

          (数据是对我的 ajax 请求的响应)

        2. 用新的改变你的网格商店:

          grid.store = dataStore;
          
        3. 渲染:

          grid.render();
          

        【讨论】:

          【解决方案4】:

          这将更新 Grid Store 并刷新 Dojo 1.9 最新版本中的 Grid 视图

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

          【讨论】:

            【解决方案5】:

            我有一个服务器端过滤的增强网格,通过更改商店令人愉快地刷新,并显示在其他答案中。

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

            grid.setFilter(grid.getFilter());
            

            这很奇怪而且很奇怪,但如果一切都失败了......

            【讨论】:

              【解决方案6】:

              有了这个我可以更新一个特定的行。此示例适用于树形网格。

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

              【讨论】:

                猜你喜欢
                • 2013-02-23
                • 2012-04-19
                • 1970-01-01
                • 2017-02-24
                • 2014-07-29
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-07-04
                相关资源
                最近更新 更多