Jason
Jason

Reputation: 1

Kendo grid data source update approach not working

I have JSON data flowing via Pusher to a simple MVC5 website housing a Kendo Grid. The data, upon arrival, renders successfully in the grid however I'm creating and setting the data source every time. As this seems sinful, I'm trying to determine why my approach to simply updating the data source doesn't render the data.

The grid:

<div id="dashboard"></div>
<script>
    $(document).ready(function () {
        $("#dashboard").kendoGrid({
            columns: [
                { field: "SystemName", width: "50px", title: "System" },
                { field: "Description", width: "100px", title: "Description" },
                { field: "SystemStatus", width: "30px", title: "Status" }
            ],
            height: 600,
            scrollable: true,
            sortable: true,
        });
        var dataSource = new kendo.data.DataSource();
        var grid = jQuery("#dashboard").data("kendoGrid");
        grid.setDataSource(dataSource);
    })
</script>

My failed attempt to read in the data without creating and binding a new data source (body of a function call which does happen):

    var array = JSON.parse(data.updateGrid);
    var grid = jQuery("#dashboard").data("kendoGrid");
    grid.dataSource.data = array;
    grid.dataSource.read(array);            
    grid.refresh();

I have confirmed that the data arrives from Pusher correctly yet the above approach does not update the grid.

Thanks in advance for any consideration.

jbt

Upvotes: 0

Views: 3479

Answers (1)

Robin Giltner
Robin Giltner

Reputation: 3055

Use the data() method on the dataSource to set it's data.

var array = JSON.parse(data.updateGrid);
var grid = jQuery("#dashboard").data("kendoGrid");
grid.dataSource.data(array);            

You can only set the string value of data on the dataSource if the source is of XML type. Since you are using JSON, you need to call the data function and pass in the new data.

See documentation... http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#methods-data

Upvotes: 2

Related Questions