4

I've been working on this for a few hours and cannot manage to find a way to get it to work properly. I'm looking for the proper way to add the contents of an array to an existing Kendo UI DataSource. Basically I have 4 SharePoint lists and I am fetching data using DataJS from each list. I want to then display the items in a Kendo GridView but I don't want to add the items using a for statement and the add() method. I have tried using the add() method on the array directly but all this does is add the array as an object itself to the DataSource and, of course, that is not the intended behavior. I also attempted using dataSource.data.concat() but received the error:

Object doesn't support property or method 'concat'

Robert Kaucher
  • 1,841
  • 3
  • 22
  • 44

2 Answers2

12

Lets say that you have the new data in an array called newData. You can use:

var newData = [
    { ... },
    { ... },
    { ... }
];

$.merge(newData, datasource._pristine);
datasource.data(newData);
OnaBai
  • 40,767
  • 6
  • 96
  • 125
  • This doesn't seem to be working for me because my datasource is a lazy-loading Web API service. So when I do this, the datasource isn't loaded yet and I get just the new constant items I was trying to merge. Am I stuck or is there a way to make this work with a Web API service? – catfood Jun 28 '13 at 19:44
  • 2
    Be careful of the order of your `$.merge()` arguments! if the `newData `should be **ap**pended (not **pre**pended), then it should be the **second** argument. Also in my kendo version the property `_pristineData`, not `_pristine`. Finally, by setting the `datasource.data(...)` to the new values directly with .data() you will "detach" the remote service. Instead I used the `schema: { data : function(){ } }` to return data as if it came back from the remote service already `merged` – Nate Anderson Feb 09 '15 at 16:42
1

The above solution did not work for me. Suggested by a Telerik admin is the method below:

var vm = kendo.observable({
  data: new kendo.data.ObservableArray([])
});

vm.data.push.apply(vm.data, [ 1, 2, 3]);

This way results in one render for bound widgets. Found here: http://www.telerik.com/forums/passing-array-to-observablearray-push

TheyCallMeSam
  • 133
  • 10