我的extjs网格没有在本地主机的网格中显示json数据

时间:2012-04-24 14:26:40

标签: python html django json extjs

我正在使用django框架并以json格式从本地主机获取一些数据,如下所示:

[{"stu_name": "Aatir"}, {"stu_name": "Mohsin"}, {"stu_name": "Anil"}, {"stu_name": "Anwar"}, {"stu_name": "Amir"}]

现在我想使用这些数据并希望在extjs网格中显示

我的extjs文件如下:

hmak.html

<html>
<head>
    <title>Account Manager</title>

    <link rel="stylesheet" type="text/css" href="{{MEDIA_URL}}extjs/resources/css/ext-all.css">
    <script type="text/javascript" src="{{MEDIA_URL}}extjs/ext-all-debug.js"></script>
    <script type="text/javascript" src="{{MEDIA_URL}}extjs/ext-debug.js"></script>
    <script type="text/javascript" src="{{MEDIA_URL}}app.js"></script>
</head>
<body>
</body>
</html>

app.js

Ext.Loader.setConfig({ enabled: true });
Ext.application({
    requires : [ 'Ext.container.Viewport'],

    controllers: ['Users'],

    name: 'AM',
    appFolder: 'media/app',

    launch: function() {
        Ext.create('Ext.container.Viewport', {
            layout: 'fit',
            items: [
                {
                    xtype: 'userlist'
                }
            ]
        });
    }
});

Users.js(控制器)

Ext.define('AM.controller.Users', {
    extend: 'Ext.app.Controller',

    stores: ['Users'],
    models: ['User'],

    views: ['user.List', 'user.Edit'],

    init: function() {
        this.control({
            'viewport > userlist': {
                itemdblclick: this.editUser
            },
            'useredit button[action=save]': {
                click: this.updateUser
            }           
        });
    },

    updateUser: function(button) {
    var win    = button.up('window'),
        form   = win.down('form'),
        record = form.getRecord(),
        values = form.getValues();

    record.set(values);
    win.close();
    this.getUsersStore().sync();
    },

    editUser: function(grid, record) {
        var view = Ext.widget('useredit');

        view.down('form').loadRecord(record);
    }
});

List.js(视图)

Ext.define('AM.view.user.List' ,{
    extend: 'Ext.grid.Panel',
    alias : 'widget.userlist',

    store: 'Users',

    title : 'All Users',

    initComponent: function() {
        this.store = {
            fields: ["stu_name"]
        };

        this.columns = [
            {header: "stu_name",  dataIndex: "stu_name",  flex: 1}
        ];

        this.callParent(arguments);
    }

});

Edit.js(视图)

Ext.define('AM.view.user.Edit', {
    extend: 'Ext.window.Window',
    alias : 'widget.useredit',

    title : 'Edit User',
    layout: 'fit',
    autoShow: true,

    initComponent: function() {
        this.items = [
            {
                xtype: 'form',
                items: [
                    {
                        xtype: 'textfield',
                        name : "stu_name",
                        fieldLabel: "stu_name"
                    }
                ]
            }
        ];

        this.buttons = [
            {
                text: 'Save',
                action: 'save'
            },
            {
                text: 'Cancel',
                scope: this,
                handler: this.close
            }
        ];

        this.callParent(arguments);
    }
});

Users.js(存储)

Ext.define('AM.store.Users', {
    extend: 'Ext.data.Store',
    model: 'AM.model.User',

    autoLoad: true,

    proxy: {
        type: 'ajax',
        api: {
            read: 'http://127.0.0.1:8000/task/'
        },
        reader: {
            type: 'json',
            root: 'users',
            successProperty: 'success'
        }
    }
});

user.js的(模型)

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

    fields: ["stu_name"]
});

viewport.js

Ext.define('SI.view.Viewport', {
    extend: 'Ext.container.Viewport'
});

在我的python中我写道:

def homePage(request):
    StuInfo.objects.all().values_list()
    return render_to_response('hmak.html', context_instance=RequestContext(request))

反过来转到hmak.html和localhost / task(我在代理中定义),它获取的数据是json形式,因为我已经向你展示了json数据

现在我的问题是为什么我的网格没有显示? 它只显示标题......

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

我认为你必须改变

Ext.define('SI.view.Viewport', {
    extend: 'Ext.container.Viewport'
});

Ext.define('AM.view.Viewport', {
    extend: 'Ext.container.Viewport'
});

并删除在initComponent函数内调用的商店。

答案 1 :(得分:0)

这可能是您问题的根源:

this.store = {
        fields: ["stu_name"]
};

initComponent函数中,您将覆盖在gridpanel类定义中定义的商店。你试过删除吗?