[FIX] web: apply digits option from view on float fields
Previously, only the value in the python description of the field could be used.
This commit is contained in:
committed by
Aaron Bohy
parent
6e1875ac7e
commit
160dd6a7e6
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user