This is a migrated thread and some comments may be shown as answers.

Adding a String to a DataSource that has been created from an Array

2 Answers 568 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
mgs
Top achievements
Rank 1
mgs asked on 29 Mar 2013, 09:37 AM
When creating a DataSource from an array, I can iterate the strings.
var arr = ["one", "two", "three"];
var datasource = new kendo.data.DataSource({
   data: arr
});
datasource.read();
 
var data1 = datasource.data();
for (var i = 0; i < data1.length; i++) {
   alert(data1[i]);
}
Then I add a new string to the datasource.
datasource.add("four");
var data2 = datasource.data();
for (var i = 0; i < data2.length; i++) {
   alert(data2[i]);
}
As a result, three strings and one object are returned from the datasource.

What should be added, so that the loop returns four strings?

Michael G. Schneider

2 Answers, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 29 Mar 2013, 11:55 AM
Hello Michael,

As stated in the documentation, the DataSource add method expected either an Object or a Model instance. This object will be then wrapped into an ObservableObject. Thus, passing a string will not yield correct results and is not supported. If you want to add a string value to the array you should use its push method instead. 

datasource.data().push("four");

All the best,

Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
mgs
Top achievements
Rank 1
answered on 29 Mar 2013, 12:03 PM
Hello Rosen,

thanks a lot for the answer.

So it is best, never to create a DataSource by giving it an array of strings. For being able to work with the DataSource afterwards, always an array of objects should be given.

Michael G. Schneider
Tags
Data Source
Asked by
mgs
Top achievements
Rank 1
Answers by
Rosen
Telerik team
mgs
Top achievements
Rank 1
Share this question
or