Working with arrays

4 posts, 0 answers
  1. Brian
    Brian avatar
    10 posts
    Member since:
    Nov 2010

    Posted 02 Mar 2012 Link to this post

    How are you supposed to work with arrays? I am messing with the "elements.html" example provided in the download for the beta and when I try to change the array of fruits to a new array, it doesn't work correctly (places that bind to it show the array as a comma-separated list instead of as distinct values).

    In the example, I added this method to the viewModel (bound to a button)...

        changeStuff: function(e) {
            e.preventDefault();
            this.set("fruits", ["Pear", "Peach", "Plumb"]);
        }

    When I click the button, the "Select" and "Multiple select" lists are updated, but instead of individual lines for each fruit, it shows all the fruits on the same line separated by commas.

    Also, how would I just change a single value in the list? For example, change "Banana" to "Pineapple"?
  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 05 Mar 2012 Link to this post

    Hi, 

    Arrays in Kendo MVVM are implemented by the kendo.data.ObservableArray type. Since inheriting from JavaScript arrays is not possible cross-browser we implemented an array-like interface in ObservableArray. The following methods are supported:

    • push
    • pop
    • slice
    • splice
    • shift
    • unshift

    If you want to replace an item in an ObservableArray you need to use the splice method:

    viewModel.fruits.splice(1, 1, "Pineapple");

    This should have also worked:

    this.set("fruits", ["Pear", "Peach", "Plumb"]); 

    however the set method does not make its argument observable in the Beta. We should make this work in the official release.

    Regards,
    Atanas Korchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Joshua
    Joshua avatar
    7 posts
    Member since:
    Oct 2011

    Posted 08 Jul 2012 Link to this post

    I tried push on my child node items array form my treeview in my viewmodel and it did not show up in the treeview.
    However if I used the same code to push before I called my databind method then it DID show up.

    However I need this to work after I have bound.
  4. richard
    richard avatar
    9 posts
    Member since:
    Jul 2012

    Posted 23 Jul 2012 Link to this post

    This was the best solution i could come up with, hopefully it helps someone

     var _tempObservableArray = new kendo.data.ObservableArray(this.get("observableArray"));
     _tempObservableArray.push(this.get("newValue"));  this.set("observableArray", _tempObservableArray); 
Back to Top