显示可观察数组的多个值中的连接字符串 [英] Showing a concatenated string from multiple values of observable array
问题描述
我有一个如下所示的可观察数组.
I have an observable array as follows..
var myObservableArray = ko.observableArray([
{ name: "Bungle", type: "Unknown" },
{ name: "George", type: "Unknown" },
{ name: "Zippy", type: "Unknown" }
]);
我希望从可观察数组中的元素中填充一个选择列表框,以使选项名称由名称"和类型"的字符串组成,例如"Bungle-Unknown","George-Unknown"等. 选项值只是名称".
I wish to fill a select listbox from elements in the observable array such that the option names are concatenated string of 'name' and 'type' like "Bungle-Unknown","George-Unknown",.etc The optionvalues are just 'name'.
衷心感谢您的帮助.
谢谢
推荐答案
在select控件的绑定中,您可以使用optionsText
绑定来为您的选项建立标题(请参见
In your binding on the select control, you can use the optionsText
binding to build up the caption for your options (see http://knockoutjs.com/documentation/options-binding.html, example 4). You could basically do this (assuming you have another observable called selected
to hold the selected option from your dropdown):
<select data-bind="options: myObservableArray,
optionsText: function(item) {
return item.name + '-' + item.type;
},
value: selected"></select>
这篇关于显示可观察数组的多个值中的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!