[英]Unable to print data in grid
我正在尝试将来自控制器的数据打印到表中,但无法执行此操作。 我正在使用Kendo UI template
进行打印。 我将下面的代码与我得到的错误一起附加。
<div id="example"></div>
<script>
$.ajax(
{
type: 'POST',
url: '/default1/KendoDataAjaxHandle/',
dataType: 'json',
success: function (result) {
//Get the external template definition using a jQuery selector
var template = kendo.template($("#javascriptTemplate").html());
console.log(result);
var data = template(result); //Execute the template
$("#example").html(data); //Append the result
}
})
</script>
<script id="javascriptTemplate" type="text/x-kendo-template">
<table>
<thead>
<tr>
<th>Customer ID</th>
<th>Customer name</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
# for (var i=0; i < result.length; i++){ #
<tr>
# for (var j=0; j < result[i].length; j++){ #
<td>
#= result[j] #
</td>
# } #
</tr>
# } #
</tbody>
</table>
</script>
我在上面的代码中所做的是对动作方法进行ajax调用,并在JSON中获取结果。 这是我的控制器方法:
public ActionResult KendoDataAjaxHandle([DataSourceRequest]DataSourceRequest request)
{
IQueryable<Customer> products = db.Customers;
DataSourceResult result = products.ToDataSourceResult(request);
return Json(result, JsonRequestBehavior.AllowGet);
}
我执行代码时遇到的错误是: 结果未定义。 但是在安慰ajax调用后返回的结果时,我得到了一个具有所有值的对象。
如何打印返回表中的那些值? 如果我在这里做错了,也请纠正我。 预先感谢。
尝试将模板内引用的变量名从“结果”更改为“数据”,因为Kendo在其模板执行代码中使用了该变量名。
范例: http : //dojo.telerik.com/@Stephen/CNUze
更新的示例显示了如何迭代对象的字段(基于注释): http : //dojo.telerik.com/@Stephen/oXOJU
注意:这假定以您在注释中指定的顺序列出字段。 如果字段没有按此顺序返回,则必须添加代码以将字段名称映射到列索引,而不仅仅是盲目循环。
更新2:
使用返回数据的确切格式更新了示例:
http://dojo.telerik.com/@Stephen/ipeHec
注意:您必须处理返回的数据中的CustomerAltID,但表中的表则不是。 我的示例选择将其从要处理的键列表中删除。 您如何处理它取决于您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.