user971741
user971741

Reputation:

Binding existing Kendo grid to a new JSON object

I have following kendoGrid on my page that on load receives JSON object from the specified URL.

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

$('#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 },
        ... ...
    ]
});

Upvotes: 2

Views: 14302

Answers (2)

CodingWithSpike
CodingWithSpike

Reputation: 43718

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

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 newData in my example would just be whatever data is returned from your function.

Upvotes: 7

OnaBai
OnaBai

Reputation: 40887

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

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

Upvotes: 0

Related Questions