我这里有一个计算值,
self.total_remain_percent = ko.computed(function() {
var x = 0;
var y = 0;
var z = 0;
var a = 0;
$.each(self.paymentPlan(), function (index, item) {
x += parseFloat(item.total_paid());
});
$.each(self.paymentPlan(), function (index, item) {
y += parseFloat(item.total_payment());
});
a = y-x ;
z = (a/y) * 100;
return z.toFixed(0);
});
我需要在进度条中显示它,我使用knockout observable与进度条绑定,但我无法显示它,这里是进度条的完成方式。
self.progress = ko.observable(10);
<div data-bind="progress: progress"></div>
现在,进度条中的值显示,计算值也显示为100,
<span data-bind="text : $data.total_remain_percent"></span>
但是我需要在进度条上显示这个总剩余百分比
我以这种方式尝试过,但没有用
self.progress = ko.observable(self.total_remain_percent);
和
self.progress = ko.observable(self.total_remain_percent());
需要帮助将价值放在那里。
答案 0 :(得分:1)
一般情况下,请勿调用变量float
,x
,y
和z
。
a
和
self.total_remain_percent = ko.computed(function() {
var totalPaid = 0;
var totalPayment = 0;
var remain = 0;
var remainPercent = 0;
ko.utils.arrayForEach(self.paymentPlan(), function (item) {
// Why aren't item.total_paid and item.total_payment numbers?
// There should not be any calls to parseFloat() here.
totalPaid += parseFloat(item.total_paid());
totalPayment += parseFloat(item.total_payment());
});
remain = totalPayment - totalPaid;
remainPercent = remain / totalPayment * 100;
// this is all about numbers, so let's return a number (toFixed returns strings)
return Math.round(remainPercent);
});
假设<div data-bind="progress: total_remain_percent"></div>
绑定将progress
转换为进度条表示,应该可以正常工作。