Showing a concatenated string from multiple values of observable array

时光毁灭记忆、已成空白 提交于 2019-12-11 01:58:25

问题


I have an observable array as follows..

var myObservableArray = ko.observableArray([
    { name: "Bungle", type: "Unknown" },
    { name: "George", type: "Unknown" },
    { name: "Zippy", type: "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'.

Any help is sincerely appreciated.

Thanks


回答1:


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>


来源:https://stackoverflow.com/questions/34454247/showing-a-concatenated-string-from-multiple-values-of-observable-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!