0

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

3
  • well, the fiddle seems to work according to what you want, no ? Commented Apr 25, 2013 at 17:30
  • @Jalayn y fiddle loads first 2 values when i select Type other than None Commented Apr 25, 2013 at 17:52
  • with my update does it work for you ? Commented Apr 25, 2013 at 18:10

1 Answer 1

1

First of all, if you want to return all items, just return self.items(), the slice is not needed. If you want to return items matching the filter, this should work:

self.filteredItems = ko.computed(function() {
    var myFilter = self.filter();
    if (!myFilter || myFilter == "None") {
        return self.items();
    } else {
        var tempArray = [];
        for(i=0; i<self.items().length; i++) {
            if(self.items()[i].type == myFilter) {
                tempArray.push(self.items()[i]);
            }
        }
        return tempArray;
    }
});

Check the working fiddle.


If you want to select only "Charger" and "Magnum" here it goes:

self.filteredItems = ko.computed(function() {
    var myFilter = self.filter();
    if (!myFilter || myFilter == "None") {
        return self.items();
    } else {
        return [ self.items()[1], self.items()[3] ];
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

it should only select "Charger" and "Magnum. Now it selects first 2 which it was working before.
ok, check the second function, this should do what you want. I updated the fiddle: jsfiddle.net/bx6TN/8
i have one query. Please see my updated fiddle jsfiddle.net/bx6TN/18. I have added another dropdown. I want to load values based on subscribe event of the Type dropdown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.