Loading data from a json file into extjs

左心房为你撑大大i 提交于 2019-12-12 11:28:35

问题


I have a .json file whose content is supposed to be read by Ext.data.Store and displayed by Ext.grid.Panel (extjs 4). However, no data is displayed, only the grid is. The code is:

var user = Ext.create('Ext.data.Store', {
    storeId: 'user',
    model: 'User',
    autoLoad: 'true',
    proxy: {
        type: 'ajax',
        url : '/users.json',
        reader: {type: 'json', root: 'blah'}
    }
});

 Ext.create('Ext.grid.Panel',{
    store :user,
    id : 'user',
    title: 'Users',
    columns : [ 
        {header: 'ID',     dataIndex : 'id'}, 
        {header : 'NAME',  dataIndex : 'name'}, 
        {header : 'Email', dataIndex: 'email'}
    ],
    height :300,
    width: 400,
    renderTo:'my-portlet2'
});

and my json file is:

{ blah[
  {
    "id": 1,
    "name": "Ed Spencer",
    "email": "ed@sencha.com"
  },
  {
    "id": 2,
    "name": "Abe Elias",
    "email": "abe@sencha.com"
  }
]}

Inline data gets displayed but when i am trying to read from a json file, it does not work. I am using firefox and firebug says that there is an error loading the script from my .js file. Could you please help? Thanks!


回答1:


Thanks a lot everyone. It is working fine now. The server could not find the files because the path was not proper. After experimenting a bit, I was finally able to fetch the json data from the json file by giving a proper path, like - "TestPortlet3-portlet/js/example.json". Initially, I was only giving "/example.json" or "users.json". On keeping the .json file in the correct folder and giving the correct path, I was able to display the json data :)




回答2:


Try this:

{ "blah": [
  {
    "id": 1,
    "name": "Ed Spencer",
    "email": "ed@sencha.com"
  },
  {
    "id": 2,
    "name": "Abe Elias",
    "email": "abe@sencha.com"
  }
]}


来源:https://stackoverflow.com/questions/10918030/loading-data-from-a-json-file-into-extjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!