Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add items to the data source of a kendo ui grid

I have created a kendo.data.dataSource with success, and I am able to bind it to the KendoUI Grid on my page.

But when I try to dataSource.insert(0, [a : "b"]); it removes the data that was there previously.

My example code follows:

var tempSource = new kendo.data.DataSource({
     data: [{"ID":1,"Name":"Cliente 1","NameID":"1 - Cliente 1"},{"ID":2,"Name":"Cliente 2","NameID":"2 - Cliente 2"}]
});

This is how I'm binding to the grid:

$("#association-grid").kendoGrid({
height: 99,
columns:
[
    {
        field: "ID",
        title: "ID"
    },
    {
        field: "Name",
        title: "Name"
    },
    {
        field: "NameID",
        title: "NameID"
    }
],

dataSource: tempSource
});

This is how I add a new item:

tempSource.insert(0, { ID: "John Smith", Name: "Product Description", NameID: "123 1st Street" });

If I perform the add before binding the data to the Grid, I lose the first two items that were originally on the dataSource object.

In summary: I have a pre-created dataSource binded to a Grid. I want to be able to add a new item to the dataSource, and then refresh the Grid so that the new item appears.

Thanks,

VRC

like image 555
Vasco Costa Avatar asked May 24 '12 09:05

Vasco Costa


People also ask

How do I assign data to a kendo grid?

Bind data to Kendo Grid by using AJAX Read action method. Change the datasource on change event of any HTML controls. Normally, a developer can bind the data to Grid by using AJAX Read method. This read method is ActionResult method in MVC which will return JSON DataSourceResult & direclty bind the data to Grid.

What is data source in kendo grid?

The data source of the Grid holds the items that will be rendered inside the widget. An item can be a JavaScript object which represents a valid data source configuration, a JavaScript array, or an existing kendo.

How do I add a row in kendo grid?

Adds an empty data item to the grid. In "incell" and "inline" editing mode a table row will be appended. Popup window will be displayed in "popup" editing mode.


1 Answers

try this:

dataSource.add({ name: "John Smith", description: "Product Description", address: "123 1st Street" });
like image 116
Zaheer Ahmed Avatar answered Oct 25 '22 06:10

Zaheer Ahmed