asp.net-mvc 更改剑道网格数据源使用JS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16774454/
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-09-08 02:52:46  来源:igfitidea点击:

Change kendo grid Datasource use JS

javascriptasp.net-mvckendo-uikendo-gridkendo-asp.net-mvc

提问by Std_Net

I have Kendo grid and I set data source use this

我有剑道网格,我设置数据源使用这个

.DataSource(dataSource => dataSource
                                .Ajax()
                                .PageSize(20)

                                .Read(read => read.Action("GetWorker", "Worker"))

I have button on my page and I want change datasource when I press this button(use java script). I want do somwthing like this

我的页面上有按钮,我想在按下此按钮时更改数据源(使用 java 脚本)。我想做这样的事情

.DataSource(dataSource => dataSource
                                .Ajax()
                                .PageSize(20)

                                .Read(read => read.Action("GetDisabled", "Worker"))

I try do like this

我试着这样做

var grid = $("grid").data("kenodGrid");
            grid.dataSource().read()

but I don't know what to do after grid.dataSource(). how can I change data source? Thnaks and hope for you help

但我不知道在 grid.dataSource() 之后该做什么。如何更改数据源?感谢并希望对您有所帮助

回答by Lopo

I think you should first create a new DataSource (see http://demos.kendoui.com/web/datasource/remote-data.htmlfor remote data)

我认为你应该首先创建一个新的数据源(远程数据见http://demos.kendoui.c​​om/web/datasource/remote-data.html

var dataSource = new kendo.data.DataSource({
    data: [
        { name: "John Doe", age: 33 }
    ]
});

And then append it to the grid by using the setDataSource method (http://docs.kendoui.com/api/web/grid#methods-setDataSource)

然后使用 setDataSource 方法(http://docs.kendoui.c​​om/api/web/grid#methods-setDataSource)将其附加到网格

var grid = $("#grid").data("kendoGrid");
grid.setDataSource(dataSource);

回答by Anastasios Selmanis

Since you want to change the action for your read then you can just do that. According to this questionyou could just set the dataSource Read url and refresh your grid data with something like that:

既然你想改变你的阅读动作,那么你就可以做到这一点。根据这个问题,您可以设置 dataSource Read url 并使用以下内容刷新您的网格数据:

var grid = $("#grid").data("kendoGrid");
grid.dataSource.transport.options.read.url = "newUrlPath";
grid.dataSource.read();
grid.refresh();

If you don't actually want to change your dataSource but your data and possibly get your list of items from some ajax request as json then I will write down the way I do it as an example in case someone wants it.

如果您实际上不想更改您的数据源而是您的数据,并且可能从某个 ajax 请求中获取您的项目列表作为 json 那么我将写下我的方式作为示例,以防有人需要它。

var jsonData = ... // From some ajax response
var newKendoDatasource = newKendoDS(jsonData);
$("#grid").data("kendoGrid").dataSource.data(newKendoDatasource._data);

The function is like the above pretty much

功能和上面差不多

function newKendoDS(ndata) {
    var datasource = new kendo.data.DataSource({ data: ndata });
    datasource.read(); // In order to refresh
    return datasource;
}