开发者

extJS: reading a nested JSON

I have a pretty nested JSON coming from a ldap_search() call. I would like to use this information to populate an ExtJS ComboBox, but I am facing some troubles with the reader. Apparently, I am not able to read the information that I need in the ComboBox, that is the mail address of the people, the uid and the cn

I think the whole problem lies in the store. I was trying the following code:

var store= new Ext.data.JsonStore({
        url:'search.php',   
        root: '',
        totalProperty: 'count',
        fields: [
            {name:'cn', type: 'string', mapping:'cn.0'},
            {name:'mail', type: 'string', mapping:'mail.0'},
       开发者_开发知识库     {name:'uid', type: 'string', mapping:'uid.0'}
        ]
});

but FireBug told me missing ; before statement return obj.cn.0 in ext-all.js (line 7). I tried with another, easier JSON array and it works, that is why I really think the problem lies in this part of code, especially in the mapping.

an example of JSON returned by search.php is:

{
  "count": 2,
  "0": {
    "mail": {
      "count": 1,
      "0": "Mail address not registered."
    },
    "0": "mail",
    "uid": {
      "count": 1,
      "0": "name0.surname0@domain.com"
    },
    "1": "uid",
    "cn": {
      "count": 1,
      "0": "Surname0 Name0"
    },
    "2": "cn",
    "count": 3,
    "dn": "cn=Surname0 Name0,ou=personal,dc=domain,dc=com"
  },
  "1": {
    "mail": {
      "count": 1,
      "0": "name1.surname1@domain.com"
    },
    "0": "mail",
    "uid": {
      "count": 1,
      "0": "name1.surname1"
    },
    "1": "uid",
    "cn": {
      "count": 1,
      "0": "Surname 1 Name 1"
    },
    "2": "cn",
    "count": 3,
    "dn": "cn=Surname1 Name1,ou=personal,dc=domain,dc=com"
  }
}

Thanks for your time.


Yep, that JSON structure is not going to work straight away with standard ExtJS JSONReader. Take a look at this example taken from the ExtJS API documentation on how the JSON should look like.

{
    results: 2000, // Reader's configured totalProperty
    rows: [        // Reader's configured root
        // record data objects:
        { id: 1, firstname: 'Bill', occupation: 'Gardener' },
        { id: 2, firstname: 'Ben' , occupation: 'Horticulturalist' },
        ...
    ]
}

Also, the root config option is required, you cannot leave it empty. In the above example your root would be "rows".

You are probably going to need to parse that JSON of yours into a simpler format at first, before feeding it to the JSONReader.


I was looking to do the same thing, but have one of the nested items be a field in my chart. This post kept coming up, so I thought it might be helpful to see what I did to solve the chart issue. The key to solving it is knowing that the label config exists: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.chart.Label. Using that you can override the default render of what you pass in. In this example the field is "key" (Not shown here, but my model is using the default type for 'key' (ie., not string)). The key object gets passed to renderer. Using function(t), I can now access that object like javascript and pass back the name under the object.

 json
    key : { 
            wholePath : "c:/.../fileName.txt",
            fileName : "fileName.txt",
        }
  code:
    axes: [
    {
        title: 'Values',
        type: 'Numeric',
        position: 'left',
        fields: ['value'],
        minimum: 0,
        maximum: 100,
        minorTickSteps: 1
    },
    {
        title: 'File Name',
        type: 'Category',
        position: 'bottom',
        fields: ['key'],
        label: {
            renderer: function(t) {
               var fileName = t.name;
               return fileName;
            }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜