在测试easyui中datagrid加载本地文件时,使用 loadData方法出现异常。
loadData使用的时候,提示:Cannot read property 'options' of undefined
具体如下:
jquery.easyui.min.js:10372 Uncaught TypeError: Cannot read property 'options' of undefined
at _718 (jquery.easyui.min.js:10372)
at HTMLTableElement.<anonymous> (jquery.easyui.min.js:11312)
at Function.each (jquery.min.js:2)
at n.fn.init.each (jquery.min.js:2)
at Object.loadData (jquery.easyui.min.js:11311)
at n.fn.init.$.fn.datagrid (jquery.easyui.min.js:11073)
at 表格过滤 - 副本.html:47
有人说是格式的问题。需要JSON对象,而不能是JSON格式的字符串。但没有解决。
我的测试代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Basic DataGrid - jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="../themes/icon.css">
<link rel="stylesheet" type="text/css" href="../demo.css">
</head>
<body>
<h2>Basic DataGrid</h2>
<div style="margin:20px 0;"></div>
<table id="dg" class="easyui-datagrid" title="Basic DataGrid" style="width:700px;height:250px"
data-options="singleSelect:true,collapsible:true" >
<thead>
<tr>
<th data-options="field:'itemid',width:80">Item ID</th>
<th data-options="field:'productid',width:100">Product</th>
<th data-options="field:'listprice',width:80,align:'right'">List Price</th>
<th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th>
<th data-options="field:'attr1',width:250">Attribute</th>
<th data-options="field:'status',width:60,align:'center'">Status</th>
</tr>
</thead>
</table>
<script type="text/javascript" src="../jquery.min.js"></script>
<script type="text/javascript" src="../jquery.easyui.min.js"></script>
<script type="text/javascript">
var data ={"total":28,"rows":[
{"productid":"FI-SW-01","productname":"Koi","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
{"productid":"K9-DL-01","productname":"Dalmation","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"}
]};
$('#dg').datagrid('loadData', data);
</script>
</body>
</html>
后来 发现有人说是html的加载顺序有关系。所有怀疑还没加载好table这个标签,就执行了$('#dg').datagrid('loadData', data);
所有就修改为
$(document).ready(
function (){
$('#dg').datagrid("loadData",data);
}
);
就可以啦!记录一下。