【问题标题】:Writing a Custom Reader in ExtJS在 ExtJS 中编写自定义阅读器
【发布时间】:2012-05-15 09:23:36
【问题描述】:

我是 ExtJS 的新手并且正在使用它,我正在从服务器接收以下 JSON,如下所示

{"tid":1,"action":"HelloWorld","method":"getData",
  "result": {"InstId":"1",
    "value":  [
  {"country.stateId":"101", "country.stateName":"TA"},
  {"country.stateId":"102", "country.stateName":"ABC"}, 
  {"country.stateId":"103", "country.stateName":"DEF"}]}",
                  "type":"rpc"}

我需要从上述 JSON 中提取值并填充组合框。要完成这项工作,我需要编写一个自定义阅读器来读取和填充组合框中的内容。你们中的任何人都可以帮我编写一个自定义阅读器吗?

下面是各种sn-p

Ext.define('AM.store.TestStore', {

  extend: 'Ext.data.Store',

  alias : 'widget.testStore',
  model: 'AM.model.TestModel',
  autoload:true,
  proxy: {
      type: 'direct',

      directFn: HelloWorld.getData,
      extraParams:[ {'InstId': '1', 'boxid':'id'},
                    {'InstId': '1', 'name':'states'}
   ],

   reader:{
         type:'json',
         root:'result'

  }
  });

下面是视图对象

Ext.define('AM.view.combobox.TestView' ,{
    extend: 'Ext.form.ComboBox',
    alias : 'widget.TestView',
    fieldLabel: 'States',
    store:'TestStore',
    renderTo: Ext.getBody(),
    displayField: 'country.stateName',
    valueField: 'country.stateId',
    autoselect:false

});

以下是我的模型对象

Ext.define('AM.model.TestModel', {
    extend: 'Ext.data.Model',

    alias : 'widget.TestModel',
    fields: [ {name: 'country.stateId'}, {name: 'country.stateName'}]
);

下面是我的控制器

Ext.define('AM.controller.Funds', {
     extend: 'Ext.app.Controller',
     alias : 'widget.FundsController',
     views: ['combobox.TestView'],
     stores: ['TestStore'],
     models:['TestModel']
);

谁能帮我写一个自定义的 JSON 阅读器?

谢谢
帕尼库马尔

【问题讨论】:

    标签: extjs extjs4


    【解决方案1】:

    您不需要为此编写 custom 阅读器。我认为对您的代码进行一些小的更改就足够了:

    首先,在代理定义中:

    root: 'value'
    useSimpleAccessors: true
    

    然后在模型中为您的数据:

    fields: [{
        name: 'id', mapping: 'country.stateId' }, {
        name: 'name', mapping: 'country.stateName' 
    }]
    

    应该可以的。

    【讨论】:

    • 您好,Sha 感谢您的回复。只是为了我的学习,你能帮我写一个自定义阅读器吗?请...
    • 编写自定义阅读器 == 阅读阅读器类的 ExtJs 源代码并更改您不喜欢的任何内容。
    • 嗨,Sha,你的回答让我异常。 “未捕获的类型错误:无法读取未定义的属性‘值’”
    • 那么你的 JSON 不像你发布的那样
    • 我使用了 firebug,很高兴看到上面提到的 JSON 对象被剥离为 {"InstId":"1", "value": [ {"country.stateId":"101 ", "country.stateName":"TA"}, {"country.stateId":"102", "country.stateName":"ABC"}, {"country.stateId":"103", "country.stateName ":"DEF"}]}" 被用作响应...!
    猜你喜欢
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多