Javascript 在extjs或getCount()中的存储中动态设置字段在模型中为0

Javascript 在extjs或getCount()中的存储中动态设置字段在模型中为0,javascript,model,store,extjs4.1,extjs-mvc,Javascript,Model,Store,Extjs4.1,Extjs Mvc,希望你做得很好 我有两个不同的班级和大学生 关系:大学有很多学生 大学字段:大学id、大学名称、学生总数 学生字段:stud\u id,stud\u name 我有以下json文件: { "data": [ { "uni_id": 1, "uni_name": "Gujarat University", "openingYear": 1980, "students": [

希望你做得很好

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

关系:大学有很多学生

大学字段:大学id、大学名称、学生总数 学生字段:stud\u id,stud\u 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"
                }
            ]
        }
    ]
}
我想加载大学模型中提到的总种数

fields:'total_stud', 
type:integer,
convert:function(value,record)
{
return record.students().getCount();
}
总人数是指在校学生人数

我在大学模型中尝试了以下内容

fields:'total_stud', 
type:integer,
convert:function(value,record)
{
return record.students().getCount();
}
我还设置了total_stud,但是,我得到了0 total_stud。当我看到记录。students()时,我得到了那所大学的所有学生。但是,为什么我没有得到getCount()


谢谢!:)

在我看来,学生总数是一个计算值。我认为您不应该在模型中包含此字段,因为您可以在需要显示它时始终计算正确的值

    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()
   });
我想到了两个可行的解决方案:

1。在大学模式中保持学生总数字段

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

这个选项会起作用,但似乎不对。我建议的选择是下一个

2。从大学模型中删除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()
   });
此选项与上一个选项的区别在于,我们删除了total_students字段,因此无需在大学存储中创建load listener,但现在如果需要显示大学记录,我们必须计算给定的总_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()
   });


我希望我能帮上忙。

嘿,@Alexrom……谢谢你的回复……我用了第一种方法。因为,我需要在我的不同图表中的位置数量中使用“total_stud”字段,我给你们举一个例子:大学有很多学生。如果我有total_stud字段,我将能够在我的系列饼图字段中使用total_stud,因为我想要我的饼图在不同大学中的学生百分比。