Question

I have a JSON dataSource that looks like this:

var productDataSource = new kendo.data.DataSource({
    transport: {
        read: {
            url: 'http://...',
            dataType: "json"
        }
    },
    pageSize: 10
});

And returns something like this:

{
   "ProdSet1":[
      {
         "Name": "Product 1-1",
         "Price": 20,
         "Quantity": 50,
         "Change": 4
      },
      {
         "Name": "Product 1-2",
         "Price": 14,
         "Quantity": 74,
         "Change": 5
      }
   ],
   "ProdSet2":[
      {
         "Name": "Product 2-1",
         "Price": 15,
         "Quantity": 12,
         "Change": 2
      }
   ]
}

Then I have multiple grids that use this one dataSource:

$("#prodSet1").kendoGrid({
    dataSource: productDataSource,
    columns: [
        { field: "ProdSet1[0].Name", title: "Name" },
        { field: "ProdSet1[0].Price", title: "Price" },
        { field: "ProdSet1[0].Quantity", title: "Quantity" },
        { field: "ProdSet1[0].Change", title: "Change" }
    ]
});

$("#prodSet2").kendoGrid({
    dataSource: productDataSource,
    columns: [
        { field: "ProdSet2[0].Name", title: "Name" },
        { field: "ProdSet2[0].Price", title: "Price" },
        { field: "ProdSet2[0].Quantity", title: "Quantity" },
        { field: "ProdSet2[0].Change", title: "Change" }
    ]
});

But doing { field: "ProdSet1[0].Name" ...} isn't working.

How can I reference the correct product data?

Was it helpful?

Solution

Since the collections are named in the return object, you can set the schema.data property to each ProdSet, and bind a grid to it.

I would manually fetch the data from the datasource, with a datasource.read()

var datafromService = productDataSource.read();

Documentation... http://docs.telerik.com/kendo-ui/documentation/api/framework/datasource#methods-read

Then bind each grid to that datafromService, with each specifying the collection inside the JSON object to bind to.

$("#prodSet1").kendoGrid({
  dataSource: {
    data: datafromService,
    schema: {
      data: 'ProdSet1' 
    }
  },
  columns: [
    { field: "Name", title: "Name" },
    { field: "Price", title: "Price" },
    { field: "Quantity", title: "Quantity" },
    { field: "Change", title: "Change" }
  ]
});

and

$("#prodSet2").kendoGrid({
  dataSource: {
    data: datafromService,
    schema: {
      data: 'ProdSet2' 
    }
  },
  columns: [
    { field: "Name", title: "Name" },
    { field: "Price", title: "Price" },
    { field: "Quantity", title: "Quantity" },
    { field: "Change", title: "Change" }
  ]
});

Now they will be bound to the same set of data, just displaying different collections from the JSON data.

See sample... http://jsbin.com/dokub/1/edit

If you are needing full CRUD operations, that gets into another bag of cats.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top