【问题标题】:set field dynamically in the store in extjs or getCount() gives 0 in model在 extjs 或 getCount() 的商店中动态设置字段在模型中给出 0
【发布时间】:2013-01-13 21:20:51
【问题描述】:

希望你做得很好。

我有 2 个不同班级的大学和学生

关系:大学有很多学生

大学字段:uni_id、uni_name、total_students 学生字段:stud_id,stud_name

我有以下 json 文件:

{
    "data": [
        {
            "uni_id": 1,
            "uni_name": "Gujarat University",
            "openingYear": 1980,
            "students": [
                {

                    "stud_id": 1,
                    "stud_name": "Rishabh Shah"
                }, {

                    "stud_id": 2,
                    "stud_name": "Rakesh Prajapati"
                }, {

                    "stud_id": 3,
                    "stud_name": "Vaibhavi Shah"
                }, {

                    "stud_id": 4,
                    "stud_name": "Jitu Mishra"
                }, {

                    "stud_id": 5,
                    "stud_name": "Sonu Nigam"
                }, {

                    "stud_id": 6,
                    "stud_name": "K.K."
                }, {

                    "stud_id": 7,
                    "stud_name": "Amitabh Bachan"
                }
            ]
        }, {
            "uni_id": 2,
            "uni_name": "Saurastra University",
            "openingYear": 1985,
            "students": [
                {

                    "stud_id": 1,
                    "stud_name": "Rakhi Sawant"
                }, {

                    "stud_id": 2,
                    "stud_name": "Smit Patel"
                }, {

                    "stud_id": 3,
                    "stud_name": "Kashyap Thaker"
                }
            ]
        }, {
            "uni_id": 3,
            "uni_name": "North Gujarat University",
            "openingYear": 1989,
            "students": [
                {

                    "stud_id": 1,
                    "stud_name": "Angellina Jollie"
                }, {

                    "stud_id": 2,
                    "stud_name": "Parag Raval"
                }, {

                    "stud_id": 3,
                    "stud_name": "Harshal Patel"
                }, {

                    "stud_id": 4,
                    "stud_name": "Harsha Bhogle"
                }, {

                    "stud_id": 5,
                    "stud_name": "Lata Mangeshkar"
                }
            ]
        }, {
            "uni_id": 4,
            "uni_name": "South Gujarat University",
            "openingYear": 1989,
            "students": [
                {

                    "stud_id": 1,
                    "stud_name": "Khushbu Shah"
                }, {

                    "stud_id": 2,
                    "stud_name": "Piyush"
                }, {

                    "stud_id": 3,
                    "stud_name": "Sakira"
                }, {

                    "stud_id": 4,
                    "stud_name": "Salman Khan"
                }, {

                    "stud_id": 5,
                    "stud_name": "Kishor Kumar"
                }, {

                    "stud_id": 6,
                    "stud_name": "Mohhamad Rafi"
                }, {

                    "stud_id": 7,
                    "stud_name": "V.V.S. Lakshman"
                }, {

                    "stud_id": 8,
                    "stud_name": "Rahul Dravid"
                }, {

                    "stud_id": 9,
                    "stud_name": "Shane Worn"
                }, {

                    "stud_id": 10,
                    "stud_name": "Neel Armstrong"
                }
            ]
        }
    ]
}

我想加载大学模型中提到的total_stud

total_stud 没有。大学里的学生..

我尝试在大学模型中进行以下操作。

fields:'total_stud', 
type:integer,
convert:function(value,record)
{
return record.students().getCount();
}

我也设置了 total_stud,但是,我得到 0 total_stud。当我看到 record.students() 时,我得到了该大学的所有学生..但是,为什么我没有得到 getCount()。

谢谢! :)

【问题讨论】:

    标签: javascript model store extjs4.1 extjs-mvc


    【解决方案1】:

    在我看来,total_students 字段是一个计算值。我认为你不应该在你的模型中包含这个字段,因为当你需要显示它时,你总是可以计算出正确的值。

    为此我想到了两种可能的解决方案:

    1.在大学模型中保留 total_students 字段

        Ext.define("University", {
        extend: 'Ext.data.Model',
        fields: ['uni_id', 'uni_name', 'openingYear','total_students'],
        hasMany: {
          model: 'Student',
          name: 'students'
        },
        });
    
        Ext.define("Student", {
        extend: 'Ext.data.Model',
        fields: ['stud_id', 'stud_name']
        });
    
    
        /* Here we create the store. The data variable is 
           the same json that you posted.  */
        var store = Ext.create('Ext.data.Store', {
        model: "University",
        autoLoad: true,
        data: data,
    
        proxy: {
          type: 'memory',
          reader: {
              type: 'json'
          }
        },
    
        listeners: {
        /* This listener activates every time the store is loaded and what it does,
           is iterate through each University record and updates the 
           'total_students' field with the correct value */
          load: function (store, records) {
              store.each(function (record) {
                  record.set('total_students', record.students().getCount());
              });
           }
         }
        });
    
        Ext.create('Ext.grid.Panel', {
        title: 'Universities',
        store: store,
        viewConfig: {
             markDirty: false
        },
        columns: [
             {text: 'ID',dataIndex: 'uni_id'},
             {text: 'Name',dataIndex: 'uni_name',flex: 1},
             {text: 'Opening Year',dataIndex: 'openingYear'},
             {text: 'Total Students',dataIndex: 'total_students'}
        ],
       height: 200,
       width: 500,
       renderTo: Ext.getBody()
       });
    

    http://jsfiddle.net/alexrom7/386ab/4/

    这个选项会起作用,但它似乎不正确。我推荐的选项是下一个。

    2。从大学模型中删除 total_students 字段

    此选项与上一个选项的区别在于,我们删除了 total_students 字段,因此无需在 University Store 中创建负载侦听器,但现在我们必须根据需要计算给定 University 记录的 total_students显示它。

        Ext.define("University", {
        extend: 'Ext.data.Model',
        fields: ['uni_id', 'uni_name', 'openingYear'],
        hasMany: {
          model: 'Student',
          name: 'students'
        },
        });
    
        Ext.define("Student", {
        extend: 'Ext.data.Model',
        fields: ['stud_id', 'stud_name']
        });
    
    
        /* Here we create the store. The data variable is the same json that
           you posted.  */
        var store = Ext.create('Ext.data.Store', {
        model: "University",
        autoLoad: true,
        data: data,
    
        proxy: {
          type: 'memory',
          reader: {
              type: 'json'
          }
        }
        });
    
    
    
        Ext.create('Ext.grid.Panel', {
        title: 'Universities',
        store: store,
        columns: [
             {text: 'ID',dataIndex: 'uni_id'},
             {text: 'Name',dataIndex: 'uni_name',flex: 1},
             {text: 'Opening Year',dataIndex: 'openingYear'},
             {
              text: 'Total Students',
               renderer: function (val, meta, record) {
                   return record.students().getCount();
               }
             }
        ],
       height: 200,
       width: 500,
       renderTo: Ext.getBody()
       });
    

    http://jsfiddle.net/alexrom7/cL9wf/9/

    希望对你有所帮助。

    【讨论】:

    • 嘿,@Alexrom....谢谢您的回复..我使用了第一种方式。因为,我需要“total_stud”字段。我的不同图表中的地方我给你一个例子:大学有很多学生..如果我有 total_stud 字段,我将能够在我的饼图中的系列字段中使用 total_stud,因为我想要我的饼图不同的学生百分比大学。
    猜你喜欢
    • 2011-11-18
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 2018-05-04
    • 2019-07-15
    • 1970-01-01
    相关资源
    最近更新 更多