使用Store,无法在ExtJS 4中正确呈现GRID

时间:2012-03-29 12:32:00

标签: extjs extjs4 extjs-mvc extjs4.1

这是HTML文件的Src代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4 /strict.dtd">
<html>
<head>
<title>MVC Architecture</title>
<link rel="stylesheet" type="text/css" href="/bh/extjs/resources/css/ext-all.css" />
<script type="text/javascript" src="extjs/ext-debug.js"></script>
<script type="text/javascript" src="Main.js"></script>
</head>
<body>
</body>
</html>

文件路径:/bh/Main.js [主文件]

Ext.require('Ext.container.Viewport');
Ext.application({
name: 'App',
appFolder: 'app',
controllers: ['UserController'],

launch: function() {

Ext.create('Ext.container.Viewport', {
layout: 'border',
items: [
        {
            xtype: 'userList'
        }
       ]
  });
 }
});

文件路径:/app/controller/UserController.js [Controller]

Ext.define('App.controller.UserController',{
 extend: 'Ext.app.Controller',
 stores: ['UserStore'],
 models:['UserModel'],
 views:['user.UserList'],
 init: function() {
  this.getUserStoreStore().load();
  }
});

文件路径:/app/store/UserStore.js

 Ext.define('App.store.UserStore', {
  extend: 'Ext.data.Store',
  model: 'App.model.UserModel',

  proxy: {
   type: 'ajax',
   url: 'app/data/contact.json'
  }
});

文件路径:/app/model/UserModel.js [Model]

Ext.define('App.model.UserModel',{
extends:'Ext.data.Model',
fields:[
    {name: 'name', type: 'string'},
    {name: 'age', type: 'string'},
    {name: 'phone', type: 'string'},
    {name: 'email', type: 'string'}
]
});

文件路径:/app/view/UserList.js [查看]

Ext.define('App.view.user.UserList' ,{
extend: 'Ext.grid.Panel',
alias:'widget.userList',
title:'Contacts',
region:'center',
resizable:true,
initComponent: function() {
this.store = 'UserStore';
this.columns = [
          {text: 'Name',flex:1,sortable: true,dataIndex: 'name'},
          {text: 'Age',flex:1,sortable: true,dataIndex: 'age'},
          {text: 'Phone',flex:1,sortable: true,dataIndex: 'phone'},
          {text: 'Email',flex:1,sortable: true,dataIndex: 'email'}
        ];

this.callParent(arguments);
}

});

在fire bug中,它显示JSON响应如下:

[{
  "name": "Aswini",
   "age": "32",
   "phone": "555-555-5555",
   "email": "aswininayak@.in"
 }]

虽然我有一个有效的json响应但为什么数据没有显示。请帮忙!!!

enter image description here

2 个答案:

答案 0 :(得分:0)

我认为你的网格没有加载,因为你没有正确加载商店。你应该使用。

this.getStore('userStore').load();

而不是

this.getUserStoreStore().load();

答案 1 :(得分:0)

您应该在代理中配置您的阅读器,如下所示:

Ext.define('App.store.UserStore', {
    extend: 'Ext.data.Store',
    model: 'App.model.UserModel',
    proxy: {
        type: 'ajax',
        url: 'app/data/contact.json',
        reader: {
            type: 'json'
        }
    }
});