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

3 posts, 1 answers
  1. mgs
    mgs avatar
    163 posts
    Member since:
    Jan 2011

    Posted 29 Mar 2013 Link to this post

    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. Answer
    Rosen
    Admin
    Rosen avatar
    3237 posts

    Posted 29 Mar 2013 Link to this post

    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!
  3. Kendo UI is VS 2017 Ready
  4. mgs
    mgs avatar
    163 posts
    Member since:
    Jan 2011

    Posted 29 Mar 2013 Link to this post

    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
Back to Top