diff options
| author | Julien Mougenot <jum@odoo.com> | 2021-03-12 13:51:48 +0000 |
|---|---|---|
| committer | Nasreddin (bon) <bon@odoo.com> | 2021-03-15 14:27:56 +0000 |
| commit | e795ce5bff14b6b748c1f4a2651946aafdc299f4 (patch) | |
| tree | b1a1c3f4f49f4c71503cdcf6446914b35c2b55a5 /addons/web/static/src | |
| parent | d2d495bca3e91862470702f1ef07aa27c33d8e2d (diff) | |
[FIX] web: allow filtering with decimals on price filter + unit test
Issue
- Install "Sales"
- Switch to "German" language
- Go to Sales -> Products -> Products
- Try to filter on "Public Price" is equal to "2,3"
Not possible to add decimal point at the end (only in middle of number).
Cause
In case the 'decimal point' in DB params is not a dot '.',
the filter input will be considered as 'text' instead of
'number'.
In case of the 'number' type; HTML do already a pre and post
processing, including managing decimal point (who, for example,
is not included in ev.target.value if last char is a '.').
Unfortunalty, the library is not well working with other
language and not supported on every browser, therefore,
must use own logic.
In case of a 'text' type, the value will be send to 'parseFloat'
,then `parseNumber` will replace decimal_point by dot (also one the
issues since needed to display decimal_point according user language),
and `Number` will remove the decimal_point in case of '123,' -> '123',
and therefore we will not be able to write decimals ( apart of adding
the decimal point after writing the whole number...)
Solution
If user input is well parsed, store parsed value in condition.value and
set condition.displayedValue to the input value (an so without updating
input value). Else, replace input value with previous value (who should
be the condition.DisplayedValue).
opw-2463441
closes odoo/odoo#67767
Signed-off-by: bon-odoo <nboulif@users.noreply.github.com>
Diffstat (limited to 'addons/web/static/src')
| -rw-r--r-- | addons/web/static/src/js/control_panel/custom_filter_item.js | 32 | ||||
| -rw-r--r-- | addons/web/static/src/xml/base.xml | 6 |
2 files changed, 25 insertions, 13 deletions
diff --git a/addons/web/static/src/js/control_panel/custom_filter_item.js b/addons/web/static/src/js/control_panel/custom_filter_item.js index 98080f6a418..0b9b1ccb8f4 100644 --- a/addons/web/static/src/js/control_panel/custom_filter_item.js +++ b/addons/web/static/src/js/control_panel/custom_filter_item.js @@ -105,6 +105,7 @@ odoo.define('web.CustomFilterItem', function (require) { const fieldType = this.fields[condition.field].type; const genericType = FIELD_TYPES[fieldType]; const operator = FIELD_OPERATORS[genericType][condition.operator]; + // Logical value switch (genericType) { case 'id': case 'number': @@ -129,6 +130,12 @@ odoo.define('web.CustomFilterItem', function (require) { default: condition.value = ""; } + // Displayed value + if (["float", "monetary"].includes(fieldType)) { + condition.displayedValue = `0${this.DECIMAL_POINT}0`; + } else { + condition.displayedValue = String(condition.value); + } } //--------------------------------------------------------------------- @@ -235,20 +242,25 @@ odoo.define('web.CustomFilterItem', function (require) { * @param {Event} ev */ _onValueInput(condition, ev) { - const type = this.fields[condition.field].type; - if (['float', 'integer', 'id'].includes(type)) { - const previousValue = condition.value; - const parser = field_utils.parse[type === 'float' ? 'float' : 'integer']; + if (!ev.target.value) { + return this._setDefaultValue(condition); + } + let { type } = this.fields[condition.field]; + if (type === "id") { + type = "integer"; + } + if (FIELD_TYPES[type] === "number") { try { - const parsed = parser(ev.target.value || 0); - // Force parsed value in the input. - ev.target.value = condition.value = (parsed || 0); + // Write logical value into the 'value' property + condition.value = field_utils.parse[type](ev.target.value); + // Write displayed value in the input and 'displayedValue' property + condition.displayedValue = ev.target.value; } catch (err) { - // Force previous value if non-parseable. - ev.target.value = previousValue || 0; + // Parsing error: reverts to previous value + ev.target.value = condition.displayedValue; } } else { - condition.value = ev.target.value || ""; + condition.value = condition.displayedValue = ev.target.value; } } } diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml index 949c6926ebf..36b63c852ad 100644 --- a/addons/web/static/src/xml/base.xml +++ b/addons/web/static/src/xml/base.xml @@ -390,20 +390,20 @@ t-att-type="DECIMAL_POINT === '.' ? 'number' : 'text'" t-attf-title="Number using {{ DECIMAL_POINT }} as decimal separator." t-attf-pattern="[0-9]+([\\{{ DECIMAL_POINT }}][0-9]+)?" - t-att-value="condition.value or (0 + DECIMAL_POINT + 0)" + t-att-value="condition.displayedValue" t-on-input="_onValueInput(condition)" /> <input t-elif="['integer', 'id'].includes(fieldType)" class="o_input" step="1" type="number" - t-att-value="condition.value or 0" + t-att-value="condition.displayedValue" t-on-input="_onValueInput(condition)" /> <input t-else="" type="text" class="o_input" - t-att-value="condition.value or ''" + t-att-value="condition.displayedValue" t-on-input="_onValueInput(condition)" /> </span> |
