2012-05-20 170 views
1

環境:綁定嵌套JSON數據對象到easyUi的數據網格

1.EasyUI Datagrid 
2.jsp(s2sh) 
3.mysql 
//until now i can populate the datagrid plugin with json object normally but a 
    small issue. 

描述:

我得到的JSON數據對象從服務器返回,象下面這樣:

{"total":28,"rows":[{"productid":"1","attr":{"size":"10dc","color":"red&yellow"}, 
        {"productid":"2","attr":{"size":"102dc","color":"green&null"} 

與插件:

$(document).ready(function(){ 
     $('#tt').datagrid({ 
     url:'loadVerifyStream.eAction', 
     pagination:true, 
     columns:[[ 
       {field:'productid',title:'Product ID',width:100}, 
       {field:'?????',title:'Product Size',width:250}, 
       {field:'?????',title:'product Color',width:100}, 
     ]] 

}); });

我不能輸出「大小」和「顏色」 ATTRS到網格,我想

{field:'attr.size',title:'Product Size',width:250}, 
{field:'attr.color',title:'product Color',width:100}, 

沒有工作。

有誰知道如何解決這個問題? 在此先感謝。

// ----------------------------------------- 我認爲我已經解決了這個問題。

參考DataGrid的API:

{field:'color',title:'product Color',width:100, 
    formatter:function(val,rec){ 
    return rec.attr == null ? "":rec.attr.color; 

}}

回答

3

這是一個比較通用的解決方案:

說你的JSON對象如下:

[{id:3,client:{name: "John",city:'New York'}, 
[{id:4,client:{name: "Paul",city:'Paris'}] 

得到你的fomatter函數中的字段名稱,使用這個標記:

<table id="dg"> 
    <thead> 
     <tr> 
     <th field="id" width="50">ID</th> 
     <th field="not used" formatter="(function(v,r,i){return formatColumn('client.name',v,r,i);})" width="50">Client name</th> 
     <th field="not used" formatter="(function(v,r,i){return formatColumn('client.city',v,r,i);})" width="50">Client city</th> 
     </tr> 
    </thead> 
</table> 

然後定義formatColumn

function formatColumn(colName, value, row, index) { 
    return eval("row."+colName"); 
} 
1

或者你可以使用這個技巧(對easyui版本1.3.1)。 在文件jquery.easyui.min.js,去行7982, 應該有下面的代碼:

cc.push(col.formatter(_5c5,_5c2,_5c1)); 

,並用此代碼替換

cc.push(col.formatter(_5c5,_5c2,_5c1,_5c4)); 

那麼你的定義格式爲如下:

function formatColumn(value, row, index, field) { 
    // field is the field name defined for this column 
    return row[field]; // or anything you want 
}