2012-03-28 90 views
0

論壇成員模型映射問題我有JSON數據映射到我的模型的一些問題4

我的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": "[email protected]", 
     "cmpemail2": "[email protected]", 
     "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); 
    } 
}); 

我的網格面板給出的代碼我網格面板正確顯示的公司名稱,但departname沒有在網格面板中。

請在上述代碼中提示我有什麼問題。

回答

0

那麼首先你的部門屬性在JSON中返回一個部門數組,因此你不能將它映射到一個departmens.departname。我只有一個部門(我猜是因爲否則你不能使用映射),你可以改變JSON來讓部門內部只有一個對象並使用映射。

另一種解決方案是,以映射到您的商店的部門領域,並保存該字段{名稱:「部門」,映射:「部門」}裏面的物體,

{ 
        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; 
        } 
       }