简体   繁体   中英

Two Select computed Knockout

I try to binding two computed select with Knockout: - the first select(DropDownLinee) is filled when on page load - the second select(DropDownCorse) is filled when the user select an item on the first select

This is the example:

<select id="DropDownLinee" data-bind="options: ArrayLinee, optionsText: 'NomeLinea', optionsValue: 'NomeLinea', value: selectedLinea " data-toggle="dropdown"></select>

<select id="DropDownCorse" data-bind="options: ArrayCorse,  optionsText: 'CodiceCorsa', optionsValue: 'CodiceCorsa', value: selectedCorsa " data-toggle="dropdown"></select>

 function LineeViewModel() {
      var self = this;

  self.selectedLinea = ko.observable();
  self.selectedCorsa = ko.observable();

  self.ArrayLinee = ko.observableArray([]);
  self.ArrayCorse = ko.observableArray([]);

  $.getJSON('/Home/GetLines', function (data) {
    self.ArrayLinee(data);
  });

  self.ArrayCorse = ko.computed(function () {
    $.getJSON('/Home/GetRides',
    {
      LineaSelezionata: self.selectedLinea(),
      DirezioneSelezionata: $('input[name=radio4]:checked', '.areaselezione').val()
    },
    function (data) {
      debugger;
      self.ArrayCorse(data);

    });
  });
}


  lineeVM = new LineeViewModel();
  ko.applyBindings(lineeVM);

I have this error when i check to load the 'DropDownCorse': Uncaught Error: Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.

Can anyone help me to solve this problem???

Thank in advance greetings Donato

You want to use subscribe , not a computed .

  self.selectedLinea.subscribe(function (newSelection) {
    $.getJSON('/Home/GetRides',
    {
      LineaSelezionata: newSelection,
      DirezioneSelezionata: $('input[name=radio4]:checked', '.areaselezione').val()
    },
    function (data) {
      debugger;
      self.ArrayCorse(data);

    });
  });

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