简体   繁体   English

ExtJs 4将记录从JSON添加到网格

[英]ExtJs 4 Add records to the grid from JSON

I'm a new ExtJS user, and I have the following problem. 我是ExtJS的新用户,并且遇到以下问题。 I'm trying to load some data to the ExtJS grid, which is serialized to json on the server (i use django serialize() method), but i'm getting an empty grid. 我正在尝试将一些数据加载到ExtJS网格中,该网格已在服务器上序列化为json(我使用django serialize()方法),但是我得到了一个空网格。 The problem seems to be in the callback function, which loads the data to the grid, but i can't solve it. 问题似乎出在回调函数中,该函数将数据加载到网格中,但我无法解决。

Here is my code: 这是我的代码:

Controller-function 控制器功能

    renderStudentList:function(){
            var ul = this.getStore('Users');                
            ul.load({
                scope   :this,
                callback : function(records, operation, success){
                    for(i in records){
/* here, i think, should be a code that assigns values from json to the grid records */
                        console.log(records[i].get('fields').name, records[i].get('fields').email);                 
                    }       
                }       
            });
    }

json-data, which i get from the server json-data,我从服务器获取

{success:true, "students":[{"pk": 1, "model": "poll.student", "fields": {"name": "Bob", "email": "bob@mail.ua"}}, {"pk": 2, "model": "poll.student", "fields": {"name": "Sam", "email": "sam@gmail.com"}}]}

my model 我的模特

Ext.define('AM.model.User', {
    extend: 'Ext.data.Model',
    idProperty: 'pk',
    fields: [{
        name:'pk', 
        type:'integer'
    },{
        name: 'fields',
        type: 'object'
    }]
});

my store 我的商店

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

    proxy: {
        type: 'ajax',
        api: {
            read: '/ex/'            
        },
        reader: {
            idProperty: 'pk',
            type: 'json',
            root: 'students',
            successProperty: 'success'
        }
    }
});

Thanks to all! 谢谢大家!

I think your json should like this if i am not wrong: 我认为如果我没有记错的话,您的json应该会这样:

{success:true, "students":[{"pk": 1, "fields": {"name": "Bob", "email": "bob@mail.ua"}}, {"pk": 2, "fields": {"name": "Sam", "email": "sam@gmail.com"}}]}

This will be done in python something like this: after you recieve your json string: Now in your json string; 这将在python中完成,如下所示:收到json字符串后:现在在json字符串中;

actual_data = [d['students']['pk'], d['fields'] for d in json]
output = "{"
output += "success: true"
output += json.dumps(actual_data)
output += "}"
return HttpResponse(output)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM