显示可观察数组的多个值中的连接字符串

显示可观察数组的多个值中的连接字符串

本文介绍了显示可观察数组的多个值中的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的可观察数组.

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",.etcThe optionvalues are just 'name'.

衷心感谢您的帮助.

谢谢

推荐答案

在select控件的绑定中,您可以使用optionsText绑定来为您的选项建立标题(请参见 http://knockoutjs.com/documentation/options-binding.html ,示例4).您基本上可以做到这一点(假设您有另一个名为selected的可观察对象,用于保存下拉菜单中的所选选项):

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>

这篇关于显示可观察数组的多个值中的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 21:25