这是视图模型
self.choiceSelect = ko.observable();
self.selectedItems = ko.observableArray([]);
self.selectedComponent = ko.observable();
self.componentList = ko.observableArray();
self.GetData = function () {
$.ajax({
url: self.url + "GetComponent",
type: "GET",
cache: false
})
.fail(function (qxhr, status, errorThrown) {
})
.done(function (data) {
self.componentList(data);
});
};
这是代码:
<select id="report-Components" data-bind="value: selectedComponent, options: componentList, optionsText: 'componentName'"></select><br /><br />
<h4>Component Attributes</h4>
<!-- ko if: selectedComponent -->
<!-- ko if: selectedComponent().componentField -->
<div data-bind="foreach: selectedComponent().componentField">
<div class="control-group">
<div class="controls">
<input type="checkbox" data-bind="checked: $parent.selectedItems, value: $data" />
</div>
<label class="control-label">
<strong data-bind="text: fieldText"></strong>
</label>
</div>
</div>
<!-- /ko -->
<!-- /ko -->
<!-- ko if: selectedItems -->
<div data-bind="foreach: selectedItems">
<!-- ko if: fieldChoice.length > 0 -->
<label data-bind="text: fieldText"></label>
<select data-bind="options: fieldChoice, optionsText: 'choiceName', value: $data.choiceSelect"></select>
<!-- /ko -->
<!-- ko if: fieldChoice.length == 0 -->
<label data-bind="text: fieldText"></label>
<input type="text" value=""/>
<!-- /ko -->
</div>
<!-- /ko -->