我正在尝试使用通过 ajax 检索的数据填充剑道网格。这是ajax:
var model = @Html.Raw(Json.Serialize(Model));
$(document).ready(function () {
$.ajax({
url: "/Utilities/Report/Run",
data: JSON.stringify(model),
contentType: "application/json",
type: "POST",
success: function(result) {
var ds = new kendo.data.DataSource({
data: result
});
alert(result);
$("#grid").data("kendoGrid").setDataSource(ds);
},
error: function(result) {
options.error(result);
}
});
$("#grid").kendoGrid({
toolbar: ["excel", "pdf"],
excel: {
fileName: "test"
},
pdf: {
fileName: "test"
},
});
});
在 alert(result)
处,数据如下所示:
[
{"TEST":"one"},
{"TEST":"two"},
{"TEST":"three"}
]
ajax 调用似乎有效,数据对我来说看起来不错,但剑道网格没有更新,它仍然是空白的。我也没有收到任何错误。我试过将 kendoGrid 放在 ajax 成功函数中,结果相同。我试过在 DataSource 中使用 transport
和 read
来检索数据,但这一直给我一个错误:n.slice is not a function
。然而,其他人似乎认为这是因为没有定义架构。由于我正在检索的数据类型,我无法定义它。从服务器检索的数据可以有任何列/字段名称,以及任意数量的列。然而,它不是复杂的 JSON。
如何让我的网格填充这些数据?
最佳答案
我创建了一个新的数据源并将其映射到成功方法之外的现有数据源。 你能试试下面这个吗:
var newDataSource = new kendo.data.DataSource({
transport: {
read: {
url: "/Utilities/Report/Run",
dataType: "json",
data: JSON.stringify(model),
error: function (result) {
options.error(result);
}
}
}
});
var d1 = $("#grid").data("kendoGrid");
d1.dataSource.data([]);
d1.setDataSource(newDataSource );
如果我遗漏了任何内容,请根据您的需要进行更改。 Kendo 数据绑定(bind)总是令人困惑 :D
关于javascript - 剑道网格 : populating with data from ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45599103/