1. 程式人生 > >easyui datagrid資料表格載入複雜json資料

easyui datagrid資料表格載入複雜json資料

在easyui官網上給出的json資料是比較基礎的一種json的資料格式,但是在實際的專案中,會出現許多複雜的json格式,這個就需要利用一些其他的方法了,主要方法是採用formatter函式來解決。

  由於本人語言表達不夠精確,先上一個json案例。
{
    "total": 2,
    "rows": [
        {
            "delStatus": 0,
            "cRoules": {
                "id": null,
                "delStatus": null,
                "remark
": null, "roulesID": null, "motorcadeID": null, "roulesName": "518", "startAdress": null, "endAdress": null }
, "busTypetemplate": { "bustemplateID": null, "templateName
": null, "busType": "比來迪5", "busBrand": "奔跑", "seatCount": "23", "wheelCount": 56 }
, "leaveFactoryTime": null }, { "delStatus": 0, "cRoules": { "id": null, "delStatus
": null, "remark": null, "roulesID": null, "motorcadeID": null, "roulesName": "304", "startAdress": null, "endAdress": null }
, "busTypetemplate": { "bustemplateID": null, "templateName": null, "busType": "比來迪6", "busBrand": "奔跑", "seatCount": "23", "wheelCount": 56 }, "leaveFactoryTime": null } ]
}

這個就是比較複雜的json例子了。通常我們所看到的也就是這樣的:

{
    "total": 2,
    "rows": [
        {
            "busCode": null,
            "durationTime": null,
            "leaveFactoryTime": null
        },
        { 
            "busCode": null,
            "durationTime": null,
            "leaveFactoryTime": null
        }
    ]
}
對於這種格式,使用easyui簡直不要太過簡單,一句$('#xx').datagrid('loadData',data.rows);就輕鬆搞定。
但是在這裡就會發現這種方法對於busTypetemplate和cRoules這種鍵值的就不行了,原因很簡單,程式碼不是人。
在這裡就需要使用formatter這個函數了。
具體做法如下:
 $('#cmanage').datagrid({
        height: 490,
        width: 1665,
        toolbar:'#toolbar',
        pagination:'true',
        striped:'false',
        singleSelect:'true',
        fitColumns:'true',
        columns: [[{title: '沒門', field: 'busID',width:'9%',align:'center'},
            {title: '看這裡', field: 'cRoules',width:'9%',align:'center',formatter:function (value,row,index) {
              var ret='';
              console.log('hhhhhaaa',value);
              if(value!=null){
                  ret=value.roulesName;//主要是使用value值,通過console列印可以看到"cRoules": //{
               // "id": null,
               // "delStatus": null,
                //"remark": null,
                //"roulesID": null,
               // "motorcadeID": null,
               // "roulesName": "304",
               // "startAdress": null,
               // "endAdress": null
           // },

              }
              return ret;
            }},
            {title: '哈哈', field: 'plate_No',width:'8%',align:'center'},
            {title: '不告訴你', field: 'Plate_No',width:'8%',align:'center'},

        ],
        ],

    });

通過該方法便能夠處理這種json格式的資料了。

但是這種方法對於

 "busTypetemplate": {
                "bustemplateID": null,
                "templateName": null,
                "busType": "比來迪6",
                "busBrand": "奔跑",
                "seatCount": "23",
                "wheelCount": 56
            },

這種就無能為力了。
在一個jsonobject中取多個欄位的時候,如果使用上面的方法是不可行的

 {title: '哈哈', field: 'busTypetemplate',width:'8%',align:'center'},
            {title: '不告訴你', field: 'busTypetemplate',width:'8%',align:'center'},

這樣不會顯示資料,只會顯示[object,object]這種。顯然不是我們想要的。這時就需要採用formatter函式中的row屬性。
通過console列印可以看到row是指的行資料。

{title: '哈哈', field: 'seatCount',width:'8%',align:'center',formatter:function(row,value,index){
if(row){
retrun row.busTypetemplate.seatCount;
}
}},
            {title: '不告訴你', field: 'wheelCount',width:'8%',align:'center',formatter:function(row,value,index){
if(row){
retrun row.busTypetemplate.wheelCount;
}
}},

通過這種方法可以實現。

寫的不清晰的地方,歡迎留言。加以指證