KnockoutJS - Binding value of select with optgroup and javascript objects

前端 未结 3 1617
长情又很酷
长情又很酷 2020-12-03 01:30

I found an example here to create a select list with optgroups using KnockoutJS. This works fine, but I want to bind the value of the dropdown to my own javascript object, t

3条回答
  •  感情败类
    2020-12-03 01:44

    If you want to have full control over the caption, groups and options and make this work with the value binding on the select element, you'll have to make sure all the options are bound before the select's value binding is bound. Otherwise the value binding will empty the selectedOption observable once the caption option is added.

    You could for example add a custom valueAfterChildren binding, which wraps the value binding and makes sure the children are bound first. Have a look at the example below, which pre-selects "Option 5" on the slightly simplified viewmodel, before applying the bindings:

    ko.bindingHandlers['valueAfterChildren'] = {
      init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        ko.applyBindingsToDescendants(bindingContext, element);
        ko.bindingHandlers['value'].init(element, valueAccessor, allBindings, viewModel, bindingContext);
        return { controlsDescendantBindings: true };
      },
      value: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        ko.bindingHandlers['value'].update(element, valueAccessor, allBindings, viewModel, bindingContext);
      }
    }
    
    var viewModel = {
      groups: [
        {
          label: "Group 1",
          children: [{ label: "Option 1" }, { label: "Option 2" }, { label: "Option 3" }]
        }, {
          label: "Group 2",
          children: [{ label: "Option 4" }, { label: "Option 5" }, { label: "Option 6" }]
        }
      ],
      selectedOption: ko.observable()
    };
    
    viewModel.selectedOption.subscribe(function(newVal) {
      console.log(`selected ${!!newVal ? newVal.label : 'empty option'}`);
    });
    viewModel.selectedOption(viewModel.groups[1].children[1]);
    ko.applyBindings(viewModel);
    
    

提交回复
热议问题