jquery - 将嵌套 JSON 数据对象绑定(bind)到 easyUi dataGrid

标签 jquery datagrid

环境:

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/

相关文章:

javascript - 单击屏幕上的菜单/幻灯片以导航到不同的页面

javascript - 从 AJAX 调用成功返回 '0' 或 0?

javascript - 我怎样才能隐藏我的 html 源代码不被复制?

jquery - EasyUI 数据网格中的动态列

c# - WPF Datagrid Double Click Cell MVVM设计

javascript - 有问题的 jQuery 滚动代码

jquery - autoProcessQueue=false 在 dropzone.js 中不起作用

c# - WPF 中的数据网格显示空白行和列

javascript - 设置列以在 EasyUI 数据网格上显示为排序

java - 在屏幕上展开 DataGrid 将使其所有项目可见