简体   繁体   中英

Filter knockout observable array

In my current project i am using an underscore filter which works on my fiddle but doesnt work for my current project, what I would like to do is use a knockout filter instead but i am not sure how to do so.

here is what i have using an underscore filter

html

<div data-bind="foreach: items">
<div class="profile" data-bind="text: name, click: $parent.clicked, enable: active,  css:{highlight: active()}"></div>
</div>

<hr>
<h2>Selected Card</h2>
<div data-bind="foreach: selectedItems">
<div data-bind="text: name"></div>
 </div>
<input type="button" data-bind="click: save" value="Save">

css

.profile {
width: 50px;
height: 80px;
color: black;
background-color:silver;
border: 1px solid black;
float: left;
line-height:80px;
text-align: center;    
margin: 2px;
}
.highlight {
background: yellow !important;
border:1px solid #000;
color: black;
}

javascript

 function Card(number) {
this.active = ko.observable(false);
this.name = ko.observable(number);
}

var model = function () {
var cards = [1, 2, 3, 5, 8, 13, 20, 40, 100];
for (var i = 0 ; i < cards.length ; i++)
    cards[i] = new Card(cards[i]);
var items = ko.observableArray(cards)
var selectedItems = ko.computed(function () {
    return _.filter(items(), function (item) {
        return item.active();
    });
})

var clicked = function (item) {
    items().forEach(function (item) { item.active(false) });
    item.active(!this.active());
};

var save = function () {
    alert("sending items \n" + ko.toJSON(selectedItems()));
}

return {
    items: items,
    selectedItems: selectedItems,
    save: save,
    clicked: clicked
}
}



ko.applyBindings(model);

and here is the fiddle http://jsfiddle.net/grahamwalsh/6RnXM/

You might get confused with your this keyword if you don't set it up correctly. I suggest you to use self keyword in order to fix your confusion. Plus I saw that your revealing module pattern is missing parenthesis at the end. Here is the fixed JavaScript:

function Card(number) {
    this.active = ko.observable(false);
    this.name = ko.observable(number);
}

var model = function () {
    var self = this;
    self.cards = [1, 2, 3, 5, 8, 13, 20, 40, 100];
    for (var i = 0 ; i < cards.length ; i++)
        cards[i] = new Card(cards[i]);
    self.items = ko.observableArray(cards)
    self.selectedItems = ko.computed(function () {
        return _.filter(items(), function (item) {
            if(item.active())
            {
                return item;
            }
        });
    })

    self.clicked = function (item) {
       item.active(true);
    };

    self.save = function () {
        alert("sending items \n" + ko.toJSON(selectedItems()));
    }

    return {
        items: items,
        selectedItems: selectedItems,
        save: save,
        clicked: clicked
    }
}();



ko.applyBindings(model);

You can check your updated JSFiddle here .

In addition, if you don't want to use underscore filtering option you can use the knockout filter as well:

function Card(number) {
    this.active = ko.observable(false);
    this.name = ko.observable(number);
}

var model = function () {
    var self = this;
    self.cards = [1, 2, 3, 5, 8, 13, 20, 40, 100];
    for (var i = 0 ; i < cards.length ; i++)
        cards[i] = new Card(cards[i]);
    self.items = ko.observableArray(cards)
    self.selectedItems = ko.computed(function () {
        return ko.utils.arrayFilter(self.items(), function(item) {
           if(item.active())
           {
                return item;
           }
        });

    });

    self.clicked = function (item) {
       item.active(true);
    };

    self.save = function () {
        alert("sending items \n" + ko.toJSON(selectedItems()));
    }

    return {
        items: items,
        selectedItems: selectedItems,
        save: save,
        clicked: clicked
    }
}();



ko.applyBindings(model);

This is the JSFiddle for the second option .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM