Array Pop

2 posts, 0 answers
  1. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 11 Jul 2012 Link to this post

    Is there no easy way to pop an item from an array?

    Take this code for example"

    <div id="kendo-bound-resourcelist" data-bind="visible: isVisible" >
                        <div data-role="grid" data-sortable="true" data-bind="source: items" data-columns='["Title", "PMID", "TypeOfMaterial",""]' data-row-template="row-template"></div>
                    </div>
     
                    <script id="row-template" type="text/x-kendo-template">
                          <tr class="k-grid-edit-row">
                              <td>
                                <span data-bind="text: Title"></span>
                              </td>
                              <td>
                                <span data-bind="text: PMID"></span>
                              </td>
                              <td>
                                <span data-bind="text: TypeOfMaterial[0]"></span>
                              </td>
                              <td>
                                <input type="button" data-bind="click: deleteItem" value="Delete" />
                              </td>
                          </tr>
                    </script>

    Ok so on click we call the delete in the model
    this._viewModel = kendo.observable({
                    items: [],
                    isVisible: function () {
                        return this.get("items").length > 0 ? true : false;
                    },
                    deleteItem: function (resource) {
                        this.get("items").pop(resource);
                    }
                });

    So why doesn't this work? :/  It's just popping off the last item in the array instead of the item I'm giving it. 

    I've tried doing things like this, but end up at the same result
    var deletedId = resource.data.Id;
    var items = this.get("items");
     
    for (var i = 0; i < items.length; i++) {
        if (items[i].Id === deletedId) {
            this.get("items").pop(items[i]);
        }
    }

    What am I doing wrong?
  2. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 12 Jul 2012 Link to this post

    Ok, so clearly a big whoops on my part, pop just knocks the last element off, and thats working fine :)

    ...so how do I go about deleting an item from that array?  Can I not just say "Delete this one I've passed into the event"?

    **EDIT**
    So this worked...is this how it's supposed to be done though?
    deleteItem: function (resource) {
                        var deletedId = resource.data.Id;
                        var items = this.get("items");
     
                        for (var i = 0; i < items.length; i++) {
                            if (items[i].Id === deletedId) {
                                this.get("items").splice(i, 1);
                            }
                        }
                    }
  3. Kendo UI is VS 2017 Ready
Back to Top