From 60d519b89043f1116ff6686ec4752593c7a49e1f Mon Sep 17 00:00:00 2001 From: "Julien (juc) Castiaux" Date: Tue, 6 Nov 2018 11:45:07 +0000 Subject: [FIX] web: format function missing in percentage widget The format function was missing for the percentage widget type, so `field_utils.parse[this.formatType]` (see [1]) resolved as `undefined` which is not a function and so raised an error when the user was trying to update the field. This PR add that parsing function. Both `xx%` and `0.xx` are understood by the parser and resolve to the correct float value. opw-1895979 [1] https://github.com/odoo/odoo/blob/e220721a8ee80c02fe5aba5ed8c9f3f4ae54ea97/addons/web/static/src/js/fields/abstract_field.js#L399 closes odoo/odoo#28431 --- addons/web/static/src/js/fields/field_utils.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'addons/web/static/src/js/fields') diff --git a/addons/web/static/src/js/fields/field_utils.js b/addons/web/static/src/js/fields/field_utils.js index 9d40c88019d..bbdee83c4f9 100644 --- a/addons/web/static/src/js/fields/field_utils.js +++ b/addons/web/static/src/js/fields/field_utils.js @@ -586,6 +586,22 @@ function parseFloatTime(value) { return factor * (hours + (minutes / 60)); } +/** + * Parse a String containing a percentage and convert it to float. + * The percentage can be a regular xx.xx float or a xx%. + * + * @param {string} value + * The string to be parsed + * @returns {float} + * @throws {Error} if the value couldn't be converted to float + */ +function parsePercentage(value) { + if (value.slice(-1) === '%') { + return parseFloat(value.slice(0, -1)) / 100; + } + return parseFloat(value); +} + /** * Parse a String containing integer with language formating * @@ -668,6 +684,7 @@ return { many2one: parseMany2one, monetary: parseMonetary, one2many: _.identity, + percentage: parsePercentage, reference: parseMany2one, selection: _.identity, // todo text: _.identity, // todo -- cgit v1.2.3