我已经写了一篇文章来展示我在下面尝试完成的事情。在父(输入)支付金额输入中输入值时,要求禁用子(费用)支付金额。反之亦然,当支付金额输入儿童(费用)支付金额输入时,父母禁用。我认为我让它完全适用于父母 - 它会禁用两个子输入 - 但是当在第二个电荷输入中输入一个值时,它不会禁用父级。它也是硬编码的,所以我需要这方面的帮助:
charges()[0].payAmount() != '' || charges()[0].payAmount() != 0
我已经看到了这个 - Knockoutjs Update child when parent's observable changes - 添加订阅者,但我认为我仍然无法从那里访问/禁用这些孩子。或者我可以,用jQuery?我也看到了这个 - Is there any way to disable a bunch of form elements at once? - 使用绑定处理程序可能会在输入子项输入中的值时处理$ parent的禁用。虽然看起来很复杂。我注意到在ko enable binding documentation中你可以传递一个abitrary表达式,但你似乎无法传递任何参数,比如当前对象。并且,在函数签名上,如果我将excursion作为参数添加,我已经验证它未定义。这是我试图附加到父节点上的禁用绑定的函数,无效,因为遇到未定义:
function hasChildEntries(encounter) {
ko.utils.arrayFirst(encounter.charges(), function (charge) {
console.log(charge + ', ' + charge.payAmount() + '...');
if (charge.payAmount() != '' || charge.payAmount() != 0)
return true;
});
return false;
};
视图:
<div>
<ul class="payments">
<!-- ko foreach: excursions -->
<li class="payments">
<div class="payments">
<a href='#' class="expand glyphicon glyphicon-minus-sign" style="color:grey"></a>
<a href='#' class="collapse glyphicon glyphicon-plus-sign" style="color:grey"></a>
</div>
<div class="payments" data-bind="text: 'Excursion ID: ' + id + ' Pay Amount: '"></div>
<input class="payments" data-bind="
textInput: payAmount,
valueUpdate: 'afterkeydown',
disable: charges()[0].payAmount() != '' || charges()[0].payAmount() != 0" />
<ul class="payments bullet">
<!-- ko foreach: charges -->
<li class="payments">
<div class="payments" data-bind="text: name + ' Pay Amount: '"></div>
<input class="payments" data-bind="
textInput: payAmount,
valueUpdate: 'afterkeydown',
disable: $parent.payAmount() != '' || $parent.payAmount() != 0" />
</li>
<!-- /ko -->
</ul>
</li>
<!-- /ko -->
</ul>
</div>
查看模型/ javascript:
var viewModel = function () {
var self = this;
self.excursions = ko.observableArray().extend({ rateLimit: 0 });
function Excursion(id, name, chgAmount, payAmount, charges) {
this.id = id;
this.name = name;
this.chgAmount = chgAmount;
this.payAmount = ko.observable('');
this.charges = ko.observableArray(charges).extend({ rateLimit: 0 });
};
function Charge(id, name, date, chgAmount, payAmount) {
this.id = id;
this.name = name;
this.date = date;
this.chgAmount = chgAmount;
this.payAmount = ko.observable('');
};
self.excursions.push(new Excursion('1234', 'Excursion 1', 90.00, 0, undefined));
ko.utils.arrayFirst(self.excursions(), function (excursion) {
if (excursion.id = '1234') {
excursion.charges.push(new Charge(1, 'Trunk Bay', '02/10/2015', 50.00, 0));
excursion.charges.push(new Charge(2, 'Cinnamon Bay', '02/10/2015', 40.00, 0));
}
});
self.excursions.push(new Excursion('1235', 'Excursion 2', 80.00, 0, undefined));
ko.utils.arrayFirst(self.excursions(), function (excursion) {
if (excursion.id == '1235')
excursion.charges.push(new Charge(3, 'Coral Bay', '02/11/2015', 80.00, 0));
});
}
var vm = new viewModel();
ko.applyBindings(vm);
$(".expand").click(function () {
$(this).toggle();
$(this).next().toggle();
$(this).parent().parent().children().last().toggle();
});
$(".collapse").click(function () {
$(this).toggle();
$(this).prev().toggle();
$(this).parent().parent().children().last().toggle();
});
CSS:
.altRow:nth-child(even) td { background-color: #D8D8D8; }
ul.payments { list-style:none; float:left; width:100% }
li.payments { padding-top:10px; float:left; width:100% }
div.payments { float:left }
.expand { width:15px;height:15px; }
.collapse { width:15px;height:15px;display:none }
ul.payments.bullet {list-style-type: disc;}
input.payments { width:80px;height:20px; }
答案 0 :(得分:1)
为您的游览模型添加计算:
this.hasCharges = ko.computed(function() {
for(var i = 0, len = charges().length; i < len; i++ ) {
if( charges()[i].payAmount() ) {
return true;
}
}
return false;
});
然后在你的标记中,替换
disable: charges()[0].payAmount() != '' || charges()[0].payAmount() != 0"
与
disable: hasCharges
无论有多少孩子,这都会禁用父母
编辑:一个小提琴: http://jsfiddle.net/8j8k7h1c/33/
尽量避免在你的函数中使用'this',scoping搞砸了。我用了
var self = this;
保持计算函数可以访问费用。