Search code examples
javascriptasp.net-mvckendo-uikendo-gridkendo-asp.net-mvc

Change kendo grid Datasource use JS


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

.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


Solution

  • I think you should first create a new DataSource (see https://demos.telerik.com/kendo-ui/datasource/remote-data-binding for remote data)

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

    And then append it to the grid by using the setDataSource method (https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/methods/setdatasource)

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