javascript 将现有的 Kendo 网格绑定到新的 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19645135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:19:24  来源:igfitidea点击:

Binding existing Kendo grid to a new JSON object

javascriptjsonkendo-uikendo-grid

提问by Maven

I have following kendoGridon my page that on load receives JSONobject from the specified URL.

kendoGrid我的页面上有以下内容,加载时JSON从指定的 URL接收对象。

But later i want to bind it to some other JSONdata received from other source. Is there a way a can bind-refresh existing data containing grid with a new JSONobject?

但后来我想将它绑定JSON到从其他来源收到的其他一些数据。有没有办法可以使用新JSON对象绑定刷新包含网格的现有数据?

$('#grid').kendoGrid({
    sortable: true,
    groupable: true,
    scrollable: true,

    pageable: {
        pageSizes: 9
    },
    dataSource: {
        transport: {
            read: {
                url: "../Get/JsonData",
                dataType: "Json"
            }
        }
    },
    columns: [
        { field: "name", title: "Name", width: 100 },
        ... ...
    ]
});

回答by CodingWithSpike

You could either replace all the data in the DataSource with:

您可以将 DataSource 中的所有数据替换为:

var newData = [ "some", "data", "array" ];

var gridWidget = $('#grid').data("kendoGrid");
gridWidget.dataSource.data(newData);

Or you can give the grid a whole new DataSource (I recommend this approach):

或者你可以给网格一个全新的数据源(我推荐这种方法):

var newData = new kendo.data.DataSource({
    data: [ "some", "data", "array" ]
});

var gridWidget = $('#grid').data("kendoGrid");
gridWidget.setDataSource(newData);

and of course newDatain my example would just be whatever data is returned from your function.

当然,newData在我的示例中,只是从您的函数返回的任何数据。

回答by OnaBai

Is the structure of the new JSON the same (same columns and same data model)? If so then yes, just play with transport.reador transport.read.urland instead of defining it as a stringdefine it as a function that returns either one or the other.

新 JSON 的结构是否相同(相同的列和相同的数据模型)?如果是,那么是的,只需使用transport.readortransport.read.url和而不是将其string定义为将其定义为返回一个或另一个的函数。

Remember that transport.readmight be an object or a function. Same happens for transport.read.url

请记住,它transport.read可能是一个对象或一个函数。同样发生在transport.read.url