前台通过ajax得到的json数据,怎么放入到easyui的datatrid中

<script type="text/javascript">
window.onload = function(){
$('#dg').datagrid({
columns:[[
{field:'id',title:'车次',width:100},
{field:'startlocation',title:'始发站',width:100},
{field:'price',title:'Price',width:100,align:'right'} ]]
});
};

function search(){
//不分页了
var startlocation = document.getElementById("startlocation").value;
var endlocation = $("#endlocation").val();
var starttime = $("#starttime").val();
alert(endlocation);
$.ajax({
type: "POST",
url: "<%=request.getContextPath()%>/ticket",
data: "op=search&startlocation=" + startlocation + "&endlocation=" + endlocation +"&starttime=" + starttime,
success: function(msg){
alert( "Data Saved: " + msg );
$('#dg').datagrid('loadData',msg); //这里为什么没效果
}
});
}
</script>
</head>

<body>
<table>
<tr>
<td>始发站:</td><td><input type="text" id="startlocation" name="startlocation" class="easyui-textbox"/></td>
<td>终点站:</td><td><input type="text" id="endlocation" name="endlocation" class="easyui-textbox"/></td>
<td>开车时间:</td><td><input id="starttime" class="easyui-timespinner" style="width:90px;" name="starttime" data-options="min:'08:30',showSeconds:false"/></td>
<td><input type="button" value="搜索" onclick="search()"/></td>
</tr>
</table>
<div style="margin:20px 0;"></div>

<table id="dg" ></table>
</body>
</html>

上面alert的结果:
Data Saved: {"total":20,"rows":[{"endtime":"1970-01-01 12:30:00.0","id":"xx112","starttime":"1970-01-01 08:30:00.0","endlocation":"太原","surplus":14,"startlocation":"晋中"},{"endtime":"1970-01-01 11:30:00.0","id":"xx123","starttime":"1970-01-01 08:30:00.0","endlocation":"太原","surplus":12,"startlocation":"宁武"}]}

第1个回答  2015-04-10
你的msg现在是字符串格式的吧?把它转成json对象本回答被提问者采纳
第2个回答  2018-01-09

我和你一样的遇到这个问题,后来我在easyui手册上找到这个,就ok了

源码如下:

希望对你有用

相似回答