Knockout.js 使用Knockout,如何按id显示列表中的项目名称?

Knockout.js 使用Knockout,如何按id显示列表中的项目名称?,knockout.js,Knockout.js,当我显示下拉列表时,我可以让用户选择。如下 <select data-bind="options : $parent.items, optionsText :'name', optionsValue:'id', value : status"></select> 但是如果我想从项目id显示项目名称呢 谢谢我不确定我是否理解这个问题,请检查这是否是您想要的: <select width="50" data-bind="foreach: items"> &

当我显示下拉列表时,我可以让用户选择。如下

<select data-bind="options : $parent.items, optionsText :'name', optionsValue:'id', value : status"></select>
但是如果我想从项目id显示项目名称呢


谢谢

我不确定我是否理解这个问题,请检查这是否是您想要的:

<select width="50" data-bind="foreach: items">
   <option data-bind="value: id, text:name, attr: {name: id}"></option>
</select>
3个选项我更喜欢2:

1选择值:“名称”而不是id。根据您问题的描述,这是一个有效的解决方案,但我敢打赌这不是您想要的

小提琴:


谢谢,我只想显示列表中的值。例如,现在有一个列表colorid颜色1红色2绿色3黄色,我有一个colorid字段,它有值2,我需要显示绿色谢谢!似乎需要使用第三种方法,它是网格中的一个字段,因此使用第一种和第二种方法的性能不好。
<select data-bind="options : items, optionsText: 'name', optionsValue: 'name', value : status"></select>
<span data-bind="text: status"></span>
<select data-bind="options : items, optionsText: 'name', value : status"></select>
<span data-bind="text: status().name"></span>
var color = ko.utils.arrayFirst(colors, function (color) {
    return color.id === status;
});