TypeError: $.data(...).data.rows is undefined报错及解决方法

    xiaoxiao2021-03-25  117

    本菜鸟今天在用spring mvc 向前台传了一个JSONArray的数据,然后前端用easyui框架设置datagrid中的data,

    前端JSP中是这么写的

    <span id="num" hidden="true">${numDetail}</span> //后台传过来的数据

    JS 中是这样的

    require(['jquery','common'], function(jQuery,common){ var num = $("#num").html(); var domainUrl = "/typesFlow"; jQuery(common.currentGrid()).datagrid({ width: common._width(), //设置表格宽度 height: common._height, //设置表格高度 pageSize:common._pageSize, //默认显示多少页 url:'', //查询的action地址 data:num, idField:'guid', //指定ID列 striped:true, //间隔条纹 pageList:[common._pageSize,30,50,100], rownumbers:true, //显示行号 pagination:true, //是否分页 //右键菜单(列筛选功能,不需要可删除) onHeaderContextMenu: function(e, field){ common.createColumnMenu(e, domainUrl); }, // 获得动态列配置(不需要可删除) onBeforeLoad: function(){ common.getServerColumnMenu(domainUrl); }, columns:[[ {field:'guid',checkbox:true}, {field:'flowDate',title:"1",width:120,align:'center',sortable:true}, {field:'deptName',title:"2",width:360,align:'center',sortable:true}, {field:'modifiedNum',title:"3",width:120,align:'center',sortable:true} ]], }); //$(common.currentGrid()).datagrid("load",num) });

    此处firebug报错

    TypeError: $.data(...).data.rows is undefined TypeError: rows is undefined

    JSONArray数据的内容如下

    [{"guid":"0f8ef394-e61f-4fd3-a666-170e16d74d6b","operateTime":1487852956000,"avgPrice":5,"belongId":"33702b43-c884-440f-980a-ce147fe1f981","operater":"b120d02a-b8e3-4e7a-b63c-719382bed6f6","typeCode":"01321024","remarks":"士大夫士大夫","isMid":"1","flowId":"a8917dd8-ace2-408a-9bf5-28229ce09679","isShow":"Y","flowDate":1487779200000}, {"guid":"e7ec414d-cffb-4399-b185-4cfe094e520f","operateTime":1487852997000,"avgPrice":1,"belongId":"33702b43-c884-440f-980a-ce147fe1f981","operater":"b120d02a-b8e3-4e7a-b63c-719382bed6f6","typeCode":"1232204","remarks":"","isMid":"1","flowId":"a8917dd8-ace2-408a-9bf5-28229ce09679","flowDate":1487779200000}]

    经搜索发现datagrid中的data格式必须如下:

    {"total":2,"rows":[ {"code":"RP-LI-02","name":"Persian","price":89.50}, {"code":"AV-CB-01","name":"Amazon Parrot","price":63.50} ]}

    也就是要加上 total 和 rows

    解决:

    修改后台传过来的值的格式,拼接成datagrid的data格式也就是字符串,并且将其转换成JSON对象,一定要转换成对象!

    如下:

    var num = $("#num").html(); var json = '{"total":'+eval(num).length+',"rows":'+num+'}'; var data = $.parseJSON(json);

    如果本文对你有所帮助,请打赏2块钱,帮我买杯咖啡,继续创作,谢谢大家!

    转载请注明原文地址: https://ju.6miu.com/read-22485.html

    最新回复(0)