剔除绑定-选择第一个填充的对象 [英] Knockout binding - select first filled object
问题描述
我具有以下KnockoutJS代码,以便为Addresses Form Editor进行数据绑定,在其中我可以具有4种地址类型("home","office"等).
I have the following KnockoutJS code in order to make a databinding for a Addresses Form Editor where I can have 4 address types ("home", "office", etc).
现在一切正常,但是selectedAddress被硬编码为索引0
Right now everything is working great but the selectedAddress is being hardcoded to be the index 0
self.selectedAddress = ko.observable(self.addresses[0]);
我不想选择执行此操作,而是选择第一个填充了数据的地址对象.
Instead of doing this I want to selected the first address object that has data filled in.
关于如何执行此操作的任何线索?
Any clue on how to do this?
赞赏.
推荐答案
Knockout的实用程序函数提供了非常方便的arrayFirst
函数,该函数非常简单:
Knockout's utility function provides the very convienent arrayFirst
function, which makes this simple:
self.selectedAddress = ko.observable(
ko.utils.arrayFirst(self.addresses(), function(item) {
return item.address1().length > 0;
}));
这是更新的小提琴.
这里是 KO的实用程序功能:
这篇关于剔除绑定-选择第一个填充的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!