javascript - How to remove an item from an observableArray with KnockoutJS - Stack Overflow

The goalRemove an item from an observableArray using KnockoutJS.The problemI do not know — nothing happ

The goal

Remove an item from an observableArray using KnockoutJS.

The problem

I do not know — nothing happens.

My markup:

<button data-bind="click: Summary.remove">
    <i class="icon-ok"></i>
</button>

My remove action:

self.remove = function (item) {
    self.products.remove(item);
};

My observableArray:

self.products = ko.observableArray();

What is happening?

Nothing. The self.remove function is triggering but nothing happens to the item.

More details?

I think that the details I've passed is enough, but if you need more, just let me know.

The goal

Remove an item from an observableArray using KnockoutJS.

The problem

I do not know — nothing happens.

My markup:

<button data-bind="click: Summary.remove">
    <i class="icon-ok"></i>
</button>

My remove action:

self.remove = function (item) {
    self.products.remove(item);
};

My observableArray:

self.products = ko.observableArray();

What is happening?

Nothing. The self.remove function is triggering but nothing happens to the item.

More details?

I think that the details I've passed is enough, but if you need more, just let me know.

Share Improve this question asked Jul 5, 2013 at 19:04 Guilherme OderdengeGuilherme Oderdenge 5,0116 gold badges66 silver badges96 bronze badges 1
  • There isn't enough information here. Please post a jsfiddle demonstrating the issue and I'll be glad to help. – Ryan Rahlf Commented Jul 5, 2013 at 19:12
Add a ment  | 

1 Answer 1

Reset to default 5

assuming you are calling from inside a foreach binding, i believe you should be using the $parent context to call the remove function:

<button data-bind="click: $parent.remove">
    <i class="icon-ok"></i>
</button>

heres a sample fiddle for how I generally structure adding/removing items from a list:

http://jsfiddle/E53tc/

html

<ul data-bind="foreach: products"> 
    <li>
    <span data-bind="text: name"></span>
    <button data-bind="click: $parent.remove">Remove</button>
    </li>
</ul>
<button data-bind="click: add">Add New </button>

javscript

var product = function (data) {
    var self = this;

    self.name = ko.observable(data);
}

var vm = function () {
    var self = this;

    self.remove = function (item) {
        self.products.remove(item);
    };

    self.add = function () {
       self.products.push(new product("new product"));   
    }

    self.products = ko.observableArray();
}

var viewModel = new vm();
viewModel.products([ new product("product a"), new product("product b")]);
ko.applyBindings(viewModel);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744828894a4595964.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信