diff options
| author | David Monjoie <dmo@odoo.com> | 2017-03-07 16:39:57 +0100 |
|---|---|---|
| committer | Aaron Bohy <aab@odoo.com> | 2017-04-11 19:44:38 +0200 |
| commit | 160dd6a7e601d7b6fa2fd4b27ac08ff851deb364 (patch) | |
| tree | 7b476a045cc6d9294e0498e5b8525abe04adfa6d | |
| parent | 6e1875ac7e9a6fea9c44d00b3fe7a2c23c9bbb6f (diff) | |
[FIX] web: apply digits option from view on float fields
Previously, only the value in the python description of the field
could be used.
| -rw-r--r-- | addons/web/static/src/js/fields/basic_fields.js | 30 | ||||
| -rw-r--r-- | addons/web/static/src/js/fields/field_utils.js | 15 |
2 files changed, 36 insertions, 9 deletions
diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js index 779420b6d3d..12699658a7b 100644 --- a/addons/web/static/src/js/fields/basic_fields.js +++ b/addons/web/static/src/js/fields/basic_fields.js @@ -277,16 +277,32 @@ var FieldInteger = InputField.extend({ var FieldFloat = InputField.extend({ supportedFieldTypes: ['float'], - _renderReadonly: function () { - var precision; - var value = this.value; + + /** + * Float fields have an additional precision parameter that is read from + * either the field node in the view or the field python definition itself. + * + * @override + */ + init: function () { + this._super.apply(this, arguments); if (this.attrs.digits) { - precision = this.node.attrs.digits; + this.nodeOptions.digits = JSON.parse(this.attrs.digits); } else { - precision = this.field.digits; + this.nodeOptions.digits = this.field.digits; } - if (precision && precision.length === 2) { - value = utils.round_decimals(value, precision[1]); + }, + + /** + * Format value according to precision parameter. + * + * @override + * @private + */ + _renderReadonly: function () { + var value = this.value; + if (this.nodeOptions.digits && this.nodeOptions.digits.length === 2) { + value = utils.round_decimals(value, this.nodeOptions.digits[1]); } var $span = $('<span>').addClass('o_form_field o_form_field_number').text(this._formatValue(value)); this.$el.html($span); diff --git a/addons/web/static/src/js/fields/field_utils.js b/addons/web/static/src/js/fields/field_utils.js index 406a86ac7a2..bee1681a067 100644 --- a/addons/web/static/src/js/fields/field_utils.js +++ b/addons/web/static/src/js/fields/field_utils.js @@ -96,11 +96,22 @@ function formatDateTime(value) { * @param {Object} [field] a description of the field (returned by fields_get * for example). It may contain a description of the number of digits that * should be used. + * @param {Object} [options] additional options to override the values in the + * python description of the field. + * @param {integer} [options.digit] the number of digit that should be used, + * instead of the default digit precision in the field. * @returns {string} */ -function formatFloat(value, field) { +function formatFloat(value, field, options) { var l10n = core._t.database.parameters; - var precision = (field && field.digits) ? field.digits[1] : 2; + var precision; + if (options && options.digits) { + precision = options.digits[1]; + } else if (field && field.digits) { + precision = field.digits[1]; + } else { + precision = 2; + } var formatted = _.str.sprintf('%.' + precision + 'f', value || 0).split('.'); formatted[0] = utils.insert_thousand_seps(formatted[0]); return formatted.join(l10n.decimal_point); |
