KnockoutJS - Linking value from a input to a datalist value

后端 未结 2 1682
囚心锁ツ
囚心锁ツ 2021-02-03 13:21

I\'m generating a datalist options based on a ko observable.

function Company(company) {
    this.id = company.Id;
    this.name = company.Name;
            


        
2条回答
  •  一生所求
    2021-02-03 13:43

    would you like something like this?

    HTML

     
     
    

    JAVASCRIPT

    var Company = function (company) {
        this.id = company.Id;
        this.name = company.Name;
        this.state = company.State.name;
    }
    var viewModel = {
        selectedCompany: ko.observable(),
        companies: ko.observableArray()
    };
    
    for(var x = 0; x<5; x++){
        viewModel.companies.push(new Company({Id: x, Name:'name' + x, State: { name: 'namestate' + x}}));
    }
    ko.applyBindings(viewModel);
    

    Knockout has already some options to create this selects.. see it in documentation http://knockoutjs.com/documentation/options-binding.html

    http://knockoutjs.com/documentation/options-binding.html

提交回复
热议问题