Arnab
Arnab

Reputation: 2354

Knockoutjs switch case with drop downlist

I have two drop down lists. Based on the selected value of the first drop down list I need to populate the second drop downlist..

My html code:

<select class="form-control" data-bind="options: $root.schemas, optionsText: 'schema', value: selschema"></select>
<!-- ko switch: selschema -->
<!-- ko case:  ['Test', 'Another Test'] -->
<select class="form-control" data-bind="options: $root.testschemas, optionsText: 'schema', value: selschematest"></select>
 <!-- /ko -->
 <!-- ko case: $else -->
<select class="form-control" data-bind="options: $root.othertestschemas, optionsText: 'schema', value: selschematest"></select>
<!-- /ko -->
  <!-- /ko -->

My js code

function TestViewModel() {
     var self = this;
     self.selschema=ko.observable();
     self.selschematest=ko.observable();

    self.schemas = ko.observableArray([{ "schema": "Test" }, { "schema": "Another Test" }, { "schema": "Other Test" }]);
     self.testschemas = ko.observableArray([{ "schema": "Test1" }, { "schema": "Test2" }]);

 self.othertestschemas = ko.observableArray([{ "schema": "other Test1" }, { "schema": "other Test2" }]);

 }

ko.applyBindings(new TestViewModel());

Jsfiddle:

I'm trying to use knockout-switch-case by MichaelBest but its always going inside the else statement..

Any help is sincerely appreciated

Thanks

Upvotes: 0

Views: 373

Answers (1)

Arnab
Arnab

Reputation: 2354

<!-- ko switch: selschema().schema -->

solved the problem.. adding <pre data-bind="text: ko.toJSON($data, null, 2)"></pre> to the html showed me the way..

jsfiddle:

Upvotes: 1

Related Questions