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

Array Pop

1 Answer 48 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 2
Veteran
Steve asked on 12 Jul 2012, 12:23 AM
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?

1 Answer, 1 is accepted

Sort by
0
Steve
Top achievements
Rank 2
Veteran
answered on 12 Jul 2012, 12:11 PM
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);
                        }
                    }
                }
Tags
MVVM
Asked by
Steve
Top achievements
Rank 2
Veteran
Answers by
Steve
Top achievements
Rank 2
Veteran
Share this question
or