I really liked the live example in cartEditor. This is what I need. Well, almost what if I want to add another dropdown. For example if I want to add to an existing example another field country
. The scenario is the following: I select the country, vehicle category and according to this choose the vehicle itself. How to bind two dropdowns to a third?
Tables in the database about such:
Table vehicle
id name countryId categoryId
Table category
id name
Table country
id name
File for a live example is located at knockoutjs.com
My solution is as follows. I wanted to filter based on ID. Plan to get the lists of countries and categories from the server and then receive from the server a filtered list of products. A huge thank you to the user. With Your help and with the help of the example on the website http://knockoutjs.com/examples/cartEditor.html I solved my problem. Post here my solution:
You'd continue with the same principle:
ko.pureComputed
properties that select subsets of your data based on user input.For example, let's say your products have both a
category
and acountry
:Now, if you have two observables, bound to your UI:
You can create a
ko.pureComputed
that makes a selection of products that meet the requirements:Example:
I did what you asked for based on the approach below -
country
andfiltering
out the vehicles array on the basis ofcountryId
andcategoryId
For this, I have
subscribed
to the selected values ofcountry
andcategory
, so when either of them changes, I call the filters to update the selected vehicle so that it is updated in the third dropdown.It also includes some test results at the end of the view model definition.
P.S. - Spare me for the bad combination of data, but I guess my efforts would be enough for you to get the gist of it ;-)
Here is the fiddle