Updating Knockout.js Observable array element value

后端 未结 2 1286
失恋的感觉
失恋的感觉 2021-02-04 00:40

I need to update an observable array element value. The observable array is a collection of class objects. First I need to find out matching object by id and update some other p

2条回答
  •  孤独总比滥情好
    2021-02-04 01:22

    That's pretty simple with knockout:

    // We're looking for the Seat with this No 
    var targetNo = 2;
    
    // Search for the seat -> arrayFirst iterates over the array and returns the
    // first item that is a match (= callback returns "true")!
    var seat = ko.utils.arrayFirst(this.seats(), function(currentSeat) {
        return currentSeat.No() == targetNo; // <-- is this the desired seat?
    });
    
    // Seat found?
    if (seat) {
        // Update the "Booked" property of this seat!
        seat.Booked(true);
    }
    

    http://jsfiddle.net/2NMJX/4/

提交回复
热议问题