【问题标题】:ExtJs 4 Add records to the grid from JSONExtJs 4 从 JSON 向网格中添加记录
【发布时间】:2012-04-27 19:42:20
【问题描述】:

我是 ExtJS 的新用户,遇到以下问题。我正在尝试将一些数据加载到 ExtJS 网格中,该网格在服务器上被序列化为 json(我使用 django serialize() 方法),但我得到了一个空网格。问题似乎出在回调函数中,它将数据加载到网格中,但我无法解决。

这是我的代码:

控制器功能

    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,我从服务器获取的

{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"}}]}

我的模特

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

我的商店

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'
        }
    }
});

谢谢大家!

【问题讨论】:

    标签: django json extjs proxy grid


    【解决方案1】:

    如果我没记错的话,我认为你的 json 应该是这样的:

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

    这将在 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      相关资源
      最近更新 更多