I am using knockout to build a user interface for the following situation:
– I have an array of items. Each item has an array of item details.
– An item detail can be of one of two types: ‘Type A’ or ‘Type B’.
– If an item detail is of type of A, it can have one of two subtypes: ‘Subtype A-1’ and ‘Subtype A-2’.
– If an item detail is of type of B, it does not have a subtype.
I have the two questions/problems:
- I’d like to add/remove the ‘Detail Subtype’ dropdown based on the value of the ‘Detail Type’ dropdown. For example, if the user selects ‘Type B’, I’d like to remove the ‘Detail Subtype’ dropdown. I also would like to set value of the underlying ‘subtype’ property to null. I played with the if binding but to no avail
- In the addItemDetail method of the view model, how can I add a new item detail?
The item details are an observable array containing regular objects that contain regular properties, not observables. You’ve already created a “pm.itemDetail” constructor function, so why don’t you use that?
I’ve updated the pm.ItemDetail function a bit:
Now you’re able to use the “visible” binding (the “if” binding hides the content of a DOM node, not the node itself):
To add a new item detail, just create an instance of “pm.ItemDetail” and push it to the itemDetails array of the currently selected item:
http://jsfiddle.net/XscRT/6/