You need to combine the with binding with if (or visible ) binding, where you can specify your condition:
<div data-bind="with: selected_category"> <select data-bind="options: subcategories, optionsText: 'name', optionsCaption: 'Select', value: $parent.selected_sub_category"></select> </div>
JSFiddle demo .
Note the use of $parent in value: $parent.selected_sub_category , you need to access the "parent" object because with creates a child context.
If you do not want to display the entire div when the subsection is empty, you need to move the with and if beyond the div , because KO does not allow multiple control flows of binding to one element.
So, in this case, your HTML will look like this:
<div class="mydiv"> <select data-bind="options: subcategories, optionsText: 'name', optionsCaption: 'Select', value: $parent.selected_sub_category"> </select> </div>
JSFiddle demo .
nemesv
source share