0

希望你做得很好。

我有 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()。

谢谢 !:)

4

1 回答 1

2

在我看来,字段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 中创建负载侦听器,但现在如果需要显示大学记录,我们必须计算给定大学记录的 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/

我希望我对您有所帮助。

于 2013-02-01T05:13:45.483 回答