From 65241628f9658651bd33f75f41062a7c198de242 Mon Sep 17 00:00:00 2001 From: Jonathan Niles Date: Sat, 26 Mar 2016 13:34:48 +0100 Subject: [PATCH] feat(filters): percentage filter implementation This commit adds in a simply percentage filter for adding percent (%) signs to numbers. If the input is not a number, the percent filter does not do anything --- client/src/js/filters/percentage.js | 24 +++++++++++++++++++ .../partials/billing_services/list/list.js | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 client/src/js/filters/percentage.js diff --git a/client/src/js/filters/percentage.js b/client/src/js/filters/percentage.js new file mode 100644 index 0000000000..7e401b886f --- /dev/null +++ b/client/src/js/filters/percentage.js @@ -0,0 +1,24 @@ +angular.module('bhima.filters') +.filter('percentage', PercentageFilter); + +/** + * Percentage Filter + * + * The percentage filter accepts a number and returns it as a percentage. + */ +function PercentageFilter() { + return function percentage(number) { + + // escape if no input was passed in + if (angular.isUndefined(number)) { return ''; } + + // cast value as number (or NaN) + var value = Number(number); + + // angular's isNumber() method reports NaNs as numbers + if (!angular.isNumber(value) || isNaN(value)) { return number; } + + // return the value + a percentage sign + return value + '%'; + }; +} diff --git a/client/src/partials/billing_services/list/list.js b/client/src/partials/billing_services/list/list.js index 686d808f25..c84534b8d7 100644 --- a/client/src/partials/billing_services/list/list.js +++ b/client/src/partials/billing_services/list/list.js @@ -30,8 +30,8 @@ function BillingServicesListController($translate, BillingServices, Accounts) { { field : 'account', displayName : $translate.instant('COLUMNS.ACCOUNT'), width: '*' }, { field : 'label', displayName : $translate.instant('COLUMNS.LABEL') }, { field : 'description', displayName: $translate.instant('COLUMNS.DESCRIPTION') }, - { field : 'value', displayName : $translate.instant('COLUMNS.VALUE') }, - { field : 'created_at', displayName : $translate.instant('COLUMNS.DATE'), cellFilter:'date' }, + { field : 'value', displayName : $translate.instant('COLUMNS.VALUE'), cellFilter:'percentage', cellClass: 'text-right' }, + { field : 'created_at', displayName : $translate.instant('COLUMNS.DATE'), cellFilter:'date', cellClass: 'text-center' }, { field : 'edit', displayName: '', cellTemplate : editTemplate, width: 45 }, { field : 'delete', displayName : '', cellTemplate: deleteTemplate, width: 45 } ]