アイテムの1つのプロパティの値を使用して、監視可能な配列の値を検索しようとしています。次にいくつかのサンプルコードを示します。
HTML:
<ul data-bind="foreach: items">
<li data-bind="text:name"></li>
<li>Cousin: <span data-bind="text:related_id"></span></li>
</ul>
JS:
var item = function (data){
this.id = ko.observable(data.id);
this.name = ko.observable(data.name);
this.related_id = ko.observable(data.related_id);
}
var related_item = function(data){
this.id = ko.observable(data.id);
this.name = ko.observable(data.name);
}
var ViewModel = function(){
this.items = ko.observableArray([
new item({id:1,name:'thing1',related_id:1}),
new item({id:2,name:'thing2',related_id:2})
]);
this.related_items = ko.observableArray([
new related_item({id:1,name:'cousin it'}),
new related_item({id:2,name:'cousin fred'})
]);
}
ko.applyBindings(new ViewModel);
上記のコードをいじる:fiddle
私の質問:related_itemのnameプロパティを取得して、アイテムのrelated_idが現在表示されている場所を表示するための最良の方法は何ですか?</ p>