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

University モデルで言及されているtotal_studをロードしたい。

total_stud はありません。大学の学生の..

大学モデルで次のことを試しました。

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

total_stud も設定しましたが、total_stud は 0 です。record.students() を見ると、その大学のすべての学生を取得できます..しかし、getCount() を取得できないのはなぜですか。

ありがとう !:)

4

1 に答える 1

2

私の見方では、フィールドtotal_studentsは計算された値です。表示する必要があるときに常に正しい値を計算できるため、このフィールドをモデルに含めるべきではないと思います。

これについては、次の 2 つの解決策を考えました。

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 フィールドを削除したことです。そのため、ユニバーシティ ストアにロード リスナーを作成する必要はありませんが、表示する必要がある場合は、ユニバーシティ レコードを指定して 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 に答える