KnockoutJS ObservableArray data grouping

后端 未结 3 1363
鱼传尺愫
鱼传尺愫 2020-12-05 08:06

Does KnockoutJS have a feature whereas I could take something like:

    var myArray = ko.observableArray([
      { name: \"Jimmy\", type: \"Friend\" },
              


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 08:59

    I have simplified RP Niemeyer's version in jsfiddle to do the same without using the distinct function. Please refer here: jsfiddle


    var Person = function(name, type) {
       this.name = ko.observable(name);
       this.type = ko.observable(type);    
    }
    
    var ViewModel = function() {
        var self = this; 
        this.choices = ["Friend", "Enemy", "Other" ];
        this.people = ko.observableArray([
               new Person("Jimmy", "Friend"),
               new Person("George", "Friend"),
               new Person("Zippy", "Enemy")
        ]);
    
        this.addPerson = function() {
            self.people.push(new Person("new", "Other"));
        };
    
        this.removePerson = function(person) {
          self.people.remove(person);  
        };
    };
    
    
    ko.applyBindings(new ViewModel());
    

    Thanks Niemeyer.

提交回复
热议问题