Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

ProgressController recalculatePercentage bug #4452

Closed
JDeuker opened this issue Sep 23, 2015 · 2 comments
Closed

ProgressController recalculatePercentage bug #4452

JDeuker opened this issue Sep 23, 2015 · 2 comments

Comments

@JDeuker
Copy link
Contributor

JDeuker commented Sep 23, 2015

AngularJS v1.4.5
angular-ui-bootstrap Version: 0.13.4

What to do:
See plnkr demo: http://plnkr.co/edit/zf2jb4?p=preview
Go inside the first textbox (100) and use Backspace to reduce it to 10.

The Problem:
The Bars are recalculated wrong.

The Origin:
First the watcher for "max" is proceeded for both bars.
Next the watcher for "value" is proceeded for only the first bar.
While calculating the totalPercantage the second bar has still the wrong percentage. See:
var totalPercentage = self.bars.reduce(function (total, bar) {
return total + bar.percent;
}, 0);

The Solution:
Recalculate the bar percentage:
var totalPercentage = self.bars.reduce(function (total, bar) {
bar.percent = +(100 * bar.value / bar.max).toFixed(2); //Bugfix
return total + bar.percent;
}, 0);

@wesleycho
Copy link
Contributor

This sounds reasonable, although I'm a bit worried as to why the unit test does not capture this situation - can you file a PR with this fix?

@JDeuker
Copy link
Contributor Author

JDeuker commented Sep 23, 2015

There is also a problem with removing an item, because recalculatePercentage is still calculated with the removed one included.

I found a solution with recalculating after removing:
this.removeBar = function (bar) {
this.bars.splice(this.bars.indexOf(bar), 1);
this.bars.forEach(function (bar) { //Bugfix
bar.recalculatePercentage();
});
};

I updated the Plunker: http://plnkr.co/edit/zf2jb4?p=preview

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants