【问题标题】:ExtJS4 - Reconfiguring a grid in ASP.NET - JSON structure issueExtJS4 - 在 ASP.NET 中重新配置网格 - JSON 结构问题
【发布时间】:2012-03-22 11:16:25
【问题描述】:

事实证明,ASP.NET 的一项安全功能在这里难以扩展——当我尝试动态重新配置网格面板时,返回 JSON 响应时添加的“d”属性似乎让 ExtJS 感到困惑,导致它在以下情况下失败试图生成新的列结构。

我遵循 nicholasnet 的这个解决方案: http://www.sencha.com/forum/showthread.php?179861-Dynamic-grid-columns-store-fields

它工作得很好,直到 JSON 有效负载被包裹在“d”属性上,例如

{"d":{
    "metaData": {
        "root": "data",
        "fields": [{
            "type": "int",
            "name": "id",
            "hidden": true,
            "header": "id",
            "groupable": false,
            "dataIndex": "id"
        }, ...omitted for brevity...]
    },
    "success": true,
    "data": [{
        "id": "1",
        "controller": "Permissions",
        "description": "Allow to see permission by roles",
        "administrator": true,
        "marketing": false
    }]
  }  
}

我不知道如何告诉 ExtJS 绕过这个问题。 我尝试将 AJAX 阅读器的“root”属性设置为 "d.data",但这会导致网格显示正确的行数,但根本没有数据。

我有 JSON 中列元数据("name", "header", "dataIndex") 所需的所有属性描述符,所以我不认为 JSON 结构是原因。我目前的主要线索是事件处理程序:

    store.on
({
    'load' :
    {
         fn: function(store, records, success, operation, eOpts)
        {
            grid.reconfigure(store,store.proxy.reader.fields);
        },
        scope: this
   }
},  this);  

当我传递“d”包装的 JSON 时,historyStore.proxy.reader.fields 部分中的 fields 未定义。有人对为什么会这样或如何解决此问题有任何想法吗?

编辑:我的商店/代理

Ext.define('pr.store.Store-History', {
    extend: 'Ext.data.Store',
    model: 'pr.model.Model-History',
    proxy: {

        type: 'ajax',
        url: '/data/history.json',
        reader: {
            type: 'json',
            root: 'd'
        }
}
});

【问题讨论】:

  • 读者对象的配置是什么?
  • 我已将我的商店类添加到 OP。
  • 你可以试试阅读器reader: { type: 'json', root: 'd.data', successProperty: 'd.success' }
  • 没有骰子。我也不完全确定successProperty应该做什么,它真的没有很好的记录。

标签: asp.net json grid extjs4


【解决方案1】:
Ext.define('pr.store.Store-History', {
    extend: 'Ext.data.Store',
    model: 'pr.model.Model-History',
    proxy: {

        type: 'ajax',
        url: '/data/history.json',
        reader: {
            type: 'json',
            root: 'data',
            readRecords: function(data) {
                //this has to be before the call to super because we use the meta data in the superclass readRecords
               var rootNode = this.getRoot(data);
               if (rootNode.metaData) {
                   this.onMetaChange(rootNode.metaData);   // data used to update fields
               }

              /**
               * @deprecated will be removed in Ext JS 5.0. This is just a copy of this.rawData - use that instead
               * @property {Object} jsonData
               */
              this.jsonData = rootNode;
              return this.callParent([rootNode]);    // data used to get root element and then get data from it
          },
        }
    }
});

更新: 您没有在阅读器中获取字段,因为从数据中获取字段的默认代码不处理您的包装数据,因此您需要更改“readRecords”函数来处理您的自定义数据

【讨论】:

  • 我以前试过这个。请再次阅读OP。它产生的只是正确的行数,但没有显示数据。
  • 谢谢,这也是我的同事在调试会话后得出的结论。虽然我使用了 this.getRoot(data).metaData 而不是 data.d.data 以便我可以指定数据的根可以是什么,以防其他框架决定与 ASP.NET 一样安全;)
猜你喜欢
  • 1970-01-01
  • 2012-08-02
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多