我在 knockoutjs 网站上找到了示例。在这里,它们将值绑定到多选列表框。但他们使用的是非常简单的可观察数组。 availableCountries和selectedCountries。
<p>
Choose some countries you'd like to visit:
<select data-bind="options: availableCountries, selectedOptions: chosenCountries" size="5" multiple="true"></select>
</p>
<script type="text/javascript">
var viewModel = {
availableCountries : ko.observableArray(['France', 'Germany', 'Spain']),
chosenCountries : ko.observableArray(['Germany']) // Initially, only Germany is selected
};
// ... then later ...
viewModel.chosenCountries.push('France'); // Now France is selected too
</script>
但是我的模型太复杂了,提到我模型的部分是json格式。
"JobOrderDelivTranscript" : [{
"TranscriptType" : {
"Id" : 1,
"Name" : null,
"CreatedBy" : 0,
"CreatedDate" : "0001-01-01T00:00:00",
"ModifiedBy" : 0,
"ModifiedDate" : "0001-01-01T00:00:00",
"IsActive" : false,
"EntityStatus" : 0,
"ErrorMessage" : null,
"ExternalID" : 0,
"ExternalSystemID" : 0
},
"Id" : 1,
"Name" : null,
"CreatedBy" : 0,
"CreatedDate" : "0001-01-01T00:00:00",
"ModifiedBy" : 0,
"ModifiedDate" : "0001-01-01T00:00:00",
"IsActive" : false,
"EntityStatus" : 0,
"ErrorMessage" : null,
"ExternalID" : 0,
"ExternalSystemID" : 0
}, {
"TranscriptType" : {
"Id" : 2,
"Name" : null,
"CreatedBy" : 0,
"CreatedDate" : "0001-01-01T00:00:00",
"ModifiedBy" : 0,
"ModifiedDate" : "0001-01-01T00:00:00",
"IsActive" : false,
"EntityStatus" : 0,
"ErrorMessage" : null,
"ExternalID" : 0,
"ExternalSystemID" : 0
},
"Id" : 2,
"Name" : null,
"CreatedBy" : 0,
"CreatedDate" : "0001-01-01T00:00:00",
"ModifiedBy" : 0,
"ModifiedDate" : "0001-01-01T00:00:00",
"IsActive" : false,
"EntityStatus" : 0,
"ErrorMessage" : null,
"ExternalID" : 0,
"ExternalSystemID" : 0
}
]
这里我的“ selectedCountries ”将是 JobOrderDelivTranscript()。如果我选择第一个选项,它应该与 JobOrderDelivTranscript()[0].TranscriptType.Id 映射。在他们的示例中,他们使用的是字符串数组,但我必须绑定复杂的数据。我怎样才能做到这一点。
即使我尝试使用自定义绑定
ko.bindingHandlers['selectedCustomOptions'] = {
getSelectedValuesFromSelectNode: function (selectNode) {
var result = [];
var nodes = selectNode.childNodes;
for (var i = 0, j = nodes.length; i < j; i++) {
var node = nodes[i], tagName = ko.utils.tagNameLower(node);
if (tagName == "option" && node.selected)
result.push(ko.selectExtensions.readValue(node));
else if (tagName == "optgroup") {
var selectedValuesFromOptGroup = ko.bindingHandlers['selectedCustomOptions'].getSelectedValuesFromSelectNode(node);
Array.prototype.splice.apply(result, [result.length, 0].concat(selectedValuesFromOptGroup)); // Add new entries to existing 'result' instance
}
}
return result;
},
'init': function (element, valueAccessor, allBindingsAccessor) {
ko.utils.registerEventHandler(element, "change", function () {
var value = valueAccessor();
var valueToWrite = ko.bindingHandlers['selectedCustomOptions'].getSelectedValuesFromSelectNode(this);
ko.jsonExpressionRewriting.writeValueToProperty(value, allBindingsAccessor, 'value', valueToWrite);
});
},
'update': function (element, valueAccessor) {
if (ko.utils.tagNameLower(element) != "select")
throw new Error("values binding applies only to SELECT elements");
var newValue = ko.utils.unwrapObservable(valueAccessor());
if (newValue && typeof newValue.length == "number") {
var nodes = element.childNodes;
for (var i = 0, j = nodes.length; i < j; i++) {
var node = nodes[i];
if (ko.utils.tagNameLower(node) === "option")
ko.utils.setOptionNodeSelectionState(node, arrayIndexOf(newValue, ko.selectExtensions.readValue(node)) >= 0);
}
}
}
};
function arrayIndexOf (array, item) {
if (typeof Array.prototype.indexOf == "function")
return Array.prototype.indexOf.call(array, item);
for (var i = 0, j = array.length; i < j; i++)
if (array[i].TranscriptType.Id() === item.Id)
return i;
return -1;
}
我已经选择了选项,但 json 数据没有得到更新。
有什么简单的方法吗?
提前致谢。