javascript - 剑道网格 : populating with data from ajax

标签 javascript jquery ajax kendo-ui kendo-grid

我正在尝试使用通过 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 中使用 transportread 来检索数据,但这一直给我一个错误: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/

相关文章:

javascript - 如何在 Javascript (Smartface) 中创建透明按钮

javascript - 这里应该使用 'form',还是没有必要?

javascript - 绑定(bind)到 NG 插件事件?

javascript - 单页javascript应用——页面和页面转换管理

jquery - HTML 数据属性值 jQuery 对象

javascript - 将参数传递给 ajax onreadystatechange 回调?

javascript - 将原型(prototype)添加到现有对象

javascript - 无法使用 jQuery 更改 HTML(本地存储)

javascript - Router.post 未连接到 AJAX

javascript - 如何在谷歌地图的风格上加载一个巨大的图像?