简体   繁体   English

如何从AJAX响应数据渲染Kendo UI网格?

[英]How to render Kendo UI grid from AJAX response data?

I have this code that gets json object from a static url and then renders grid. 我有这段代码从静态网址获取json对象,然后呈现网格。 But I want to use json data retrived as AJAX response and then render grid using this response text. 但是我想使用JSON数据作为AJAX响应检索,然后使用此响应文本呈现网格。 Because for practical deployment I can't use static URL. 因为对于实际部署,我不能使用静态URL。

    $("#grid").kendoGrid({
    dataSource: {
        type: "json",
        transport: {
            read: {url: "http://url/returnsjsonobject.php"}
            //THIS GETS DATA FROM STATIC URL BUT I WANT TO READ DATA AS AJAX RESPONSE
            //like read: somefunctioncall
            //or like read: somevariable
        },
        schema: {
            model: {
                fields: {
                    id: {type: "string", editable: false},
                    name: {type: "string"}

                }
            }
        },
        pageSize: 20
    },
    height: 430
    columns: [
        {field: "id", title: "ID", width: "20px", hidden: "true"},
        "name",
});

Thanks in advance for help and if you have any alternative method; 在此先感谢您的帮助,如果您有其他替代方法,请联系我们。 I will be happy to try it. 我很乐意尝试。

Remember that transport.read.url does not have to be a constant but might be a function: 请记住, transport.read.url不必是常量,而可以是一个函数:

transport: {
    read: {
        url: function(options) {
            return "somefunctionalcall?id=" + options.id,
        },
        dataType: "json"
}

or even define transport.read as a function: 甚至将transport.read定义为一个函数:

transport: {
    read: function (options) {
        $.ajax({
            dataType: "json",
            url: "somefunctionalcall",
            success: function (d) {
                options.success(d);
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM