I have 2 dropdowns. When i select value from one dropdown it will load values in another dropdown depending upon what is selected.
Here is my html code
<select data-bind="options: filters, value: filter"></select>
<select data-bind="options: filteredItems, optionsText: 'name'"></select>
Here is my knockout code
var ViewModel = function(data) {
var self = this;
self.filters = ko.observableArray(data.filters);
self.filter = ko.observable('');
self.items = ko.observableArray(data.items);
self.filteredItems = ko.computed(function() {
var filter = self.filter();
if (!filter || filter == "None") {
return self.items.slice(0, 6);
} else {
return self.items.slice(2);
}
});
};
var initialData = {
filters: ["None", "Old", "New", "Super"],
items: [{ name: "Corvette", type: "Old"},
{ name: "Charger", type: "Old"},
{ name: "Prius", type: "New"},
{ name: "Magnum", type: "New"},
{ name: "McLaren", type: "Super"},
{ name: "Saleen", type: "Super"}]
ko.applyBindings(new ViewModel(initialData));
When i select Type as None then it selects all Cars and if i select type other than None then it should only select "Charger" and "Magnum"
Here is the link to fiddle