模型映射问题与网格面板extjs 4的子数组

时间:2012-03-28 06:11:39

标签: extjs4 extjs-mvc

论坛成员我在将json数据映射到我的模型时遇到了一些问题

我收到的json数据如下:

{
    "companydata": [{
        "cmpname": "Kintu Designs Pvt ltd.",
        "cmptitle": "Kintu Designs Pvt ltd.",
        "cmpdesc": "<b>Kintu Designs Pvt ltd.</b>",
        "cmpfax": "8128812153",
        "cmpcontact": "8128812153",
        "cmpwebsite": "www.kintudesigns.com",
        "cmpemail1": "yaryan997@gmail.com",
        "cmpemail2": "yaryan997@gmail.com",
        "cmpcountry": "India",
        "cmpstate": "Gujarat",
        "cmpcity": "Surat",
        "cmpaddress": "Kintu Designs Pvt ltd. Nanpura Surat",
        "departments": [{
            "departname": "Programmers",
            "departdescr": "<b>?Programmers</b>",
            "createdby": 1,
            "createdon": 1200022207000,
            "modifiedon": 1200022207000,
            "modifiedby": 1,
            "id": 1
        }],
        "cmplogo": "calendar.png",
        "cmplogopath": "upload/images/",
        "cmpcreatedby": 1,
        "cmpcreatedon": 1200011900000,
        "cmpmodifiedon": 1200011900000,
        "cmpmodifiedby": 0,
        "id": 1
    }],
    "total": 1,
    "success": true
}

我的公司模式是

Ext.define('rms.model.companyModel', {
    extend: 'Ext.data.Model',
    fields : [
              { name: 'id', type: 'int', useNull: true, mapping: 'id'},
              { name: 'cmpname', type: 'string', mapping: 'cmpname'},
              { name: 'cmptitle', type: 'string', mapping: 'cmptitle'},
              { name: 'cmpdesc', type: 'string', mapping: 'cmpdesc'},
              { name: 'cmpfax', type: 'string', mapping: 'cmpfax'},
              { name: 'cmpcontact', type: 'string', mapping: 'cmpcontact'},
              { name: 'cmpwebsite', type: 'string', mapping: 'cmpwebsite'},
              { name: 'cmpemail1', type: 'string', mapping: 'cmpemail1'},
              { name: 'cmpemail2', type: 'string', mapping: 'cmpemail2'},
              { name: 'cmpcountry', type: 'string', mapping: 'cmpcountry'},
              { name: 'cmpstate', type: 'string', mapping: 'cmpstate'},
              { name: 'cmpcity', type: 'string', mapping: 'cmpcity'},
              { name: 'cmplogo', type: 'string', mapping: 'cmplogo'},
              { name: 'cmplogopath', type: 'string', mapping: 'cmplogopath'},
              { name: 'cmpaddress', type: 'string', mapping: 'cmpaddress'},

              { name: 'departname', type: 'string', mapping: 'departments.departname'},
              { name: 'departdescr', type: 'string', mapping: 'departments.departdescr'},
          ]
});

但我仍然无法在网格面板中显示部门名称。 我的网格面板是下面的代码

Ext.define('rms.view.companymgt.companyDetail', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.companydetail',
    id: 'companydetail',
    itemId: 'companydetail',
    store: 'company',
    forceFit: true,
    frame: true,
    initComponent: function() {
        var me = this;


        Ext.applyIf(me, {
            viewConfig: {


            },
            columns: [{xtype: 'rownumberer', width: 40},
                {
                    xtype: 'gridcolumn',
                    dataIndex: 'cmptitle',
                    text: 'Company Title'
                },
                {
                    xtype: 'gridcolumn',
                    dataIndex: 'departname',
                    text: 'Department Name'
                }
        });


        me.callParent(arguments);
    }
});

我的网格面板正确显示公司标题,但网格面板中未显示离开名称。

请告诉我上述代码中的错误。

1 个答案:

答案 0 :(得分:0)

首先,json中的所有部门属性都返回一个部门数组,因此您无法将其映射到departmens.departname。我只有一个部门(我猜是因为你不能使用映射)你可以改变json只在部门内部有一个对象并使用映射。

另一个解决方案是映射到商店中的departments字段并将对象保存在该字段中{name:'departments',mapping:'departments'},

{
                    xtype: 'gridcolumn',
                    dataIndex: 'departname',
                    text: 'Department Name',
                    renderer: function(value){
                      //if you leave your json like you curently have it 
                      return value[0].departname;
                      //or if you have only a department object
                      return value.departname;
                    }
                }