7
하위 카테고리 드롭 다운 목록을 제어하는 카테고리 드롭 다운 목록이 있습니다. 하위 카테고리 배열이 선택된 카테고리에 대해 비어 있으면 하위 카테고리 드롭 다운 목록을 숨기고 싶습니다. 아래knockout js 'with binding, 배열이 비어 있으면 숨기기
샘플 코드 :
<div data-bind="with: selected_category">
<!-- ko if: subcategories.length > 0 -->
<select data-bind="options: subcategories, optionsText: 'name',
optionsCaption: 'Select', value: $parent.selected_sub_category"></select>
<!-- /ko -->
</div>
데모 : 당신은 with
바인딩 if
(또는 visible
) 바인딩 결합해야
<script>
self.categories = ko.observableArray([
{"name": "top 1", "subcategories":[
{"name": "sub 1"},
{"name": "sub 2"}
]},
{"name": "top 2", "subcategories":[]}
]);
self.selected_category = ko.observable();
self.selected_sub_category = ko.obserable();
</script>
<div>
<select data-bind="options: categories, optionsText: "name", optionsCaption: "Select", value: selected_category"></select>
</div>
<div data-bind="with:selected_category">
<select data-bind="options: subcategories, optionsText: "name", optionsCaption: "Select", value: selected_sub_category"></select>
</div>
감사 nemesv. with와 if를 하나의 데이터 바인드 속성에 결합하는 방법이 있습니까? 그 이유는 div가 전혀 렌더링되지 않은 경우 선호하기 때문입니다. –
'if '와'with'는 데이터 바인딩 속성에서 결합 될 수 없습니다. "여러 바인딩 (with 및 if)이 동일한 요소의 자손 바인딩을 제어하려고 시도하지만 같은 요소에서이 바인딩을 함께 사용할 수 없습니다."라는 오류 메시지가 나타납니다. 그러나 당신은 그들을 "바깥"으로 움직여서 내 대답을 보게됩니다. – nemesv