环境:
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-data-object,如下所示:
{"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},
]]
}); });
我无法将“大小”和“颜色”属性输出到网格,我尝试过
{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;
}}
最佳答案
这是一个更通用的解决方案:
假设您的 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");
}
关于jquery - 将嵌套 JSON 数据对象绑定(bind)到 easyUi dataGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10670977/