2012-05-20 124 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 
}