Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
3 Răspunsuri
11166 Vizualizări

How could I inherit/override a function in Odoo 16 javascript example 

In the addons/web/static/src/views/fields/formatters.js there is a function 

export function formatFloat(value, options = {}) {
if (value === false) {
return "";
}
if (options.humanReadable) {
return humanNumber(value, options);
}
const grouping = options.grouping || l10n.grouping;
const thousandsSep = "thousandsSep" in options ? options.thousandsSep : l10n.thousandsSep;
const decimalPoint = "decimalPoint" in options ? options.decimalPoint : l10n.decimalPoint;
let precision;
if (options.digits && options.digits[1] !== undefined) {
precision = options.digits[1];
} else {
precision = 2;
}
const formatted = (value || 0).toFixed(precision).split(".");
formatted[0] = insertThousandsSep(formatted[0], thousandsSep, grouping);
if (options.noTrailingZeros) {
formatted[1] = formatted[1].replace(/0+$/, "");
}
return formatted[1] ? formatted.join(decimalPoint) : formatted[0];
}



How could I inherit/override this function?

Imagine profil
Abandonează
Autor

Dear Ashish,

Thank you for your answer, but Unfortunately, it's not working.

Cel mai bun răspuns

i also need override this function, there is a way, it worked for me.

1. create a js file in your module.

struct like :

​module/static/src/js/formatters.js

formatters.js







/** @odoo-module **/
import { patch } from "@web/core/utils/patch";
import { localization as l10n } from "@web/core/l10n/localization";
import * as formatters from "@web/views/fields/formatters";
import { registry } from "@web/core/registry";
import { intersperse } from "@web/core/utils/strings";


function insertThousandsSep(number, thousandsSep = ",", grouping = []) {
const negative = number[0] === "-";
number = negative ? number.slice(1) : number;
return (negative ? "-" : "") + intersperse(number, grouping, thousandsSep);
}

function humanNumber(number, options = { decimals: 0, minDigits: 1 }) {
const decimals = options.decimals || 0;
const minDigits = options.minDigits || 1;
const d2 = Math.pow(10, decimals);
const numberMagnitude = +number.toExponential().split("e+")[1];
number = Math.round(number * d2) / d2;
// the case numberMagnitude >= 21 corresponds to a number
// better expressed in the scientific format.
if (numberMagnitude >= 21) {
// we do not use number.toExponential(decimals) because we want to
// avoid the possible useless O decimals: 1e.+24 preferred to 1.0e+24
number = Math.round(number * Math.pow(10, decimals - numberMagnitude)) / d2;
return `${number}e+${numberMagnitude}`;
}
// note: we need to call toString here to make sure we manipulate the resulting
// string, not an object with a toString method.
const unitSymbols = _t("kMGTPE").toString();
const sign = Math.sign(number);
number = Math.abs(number);
let symbol = "";
for (let i = unitSymbols.length; i > 0; i--) {
const s = Math.pow(10, i * 3);
if (s <= number / Math.pow(10, minDigits - 1)) {
number = Math.round((number * d2) / s) / d2;
symbol = unitSymbols[i - 1];
break;
}
}
const { decimalPoint, grouping, thousandsSep } = l10n;

// determine if we should keep the decimals (we don't want to display 1,020.02k for 1020020)
const decimalsToKeep = number >= 1000 ? 0 : decimals;
number = sign * number;
const [integerPart, decimalPart] = number.toFixed(decimalsToKeep).split(".");
const int = insertThousandsSep(integerPart, thousandsSep, grouping);
if (!decimalPart) {
return int + symbol;
}
return int + decimalPoint + decimalPart + symbol;
}


function formatFloatNoTrailingZeros(value, options = {}) {
if (value === false) {
return "";
}
if (options.humanReadable) {
return humanNumber(value, options);
}
const grouping = options.grouping || l10n.grouping;
const thousandsSep = "thousandsSep" in options ? options.thousandsSep : l10n.thousandsSep;
const decimalPoint = "decimalPoint" in options ? options.decimalPoint : l10n.decimalPoint;
let precision;
if (options.digits && options.digits[1] !== undefined) {
precision = options.digits[1];
} else {
precision = 2;
}
const formatted = (value || 0).toFixed(precision).split(".");
formatted[0] = insertThousandsSep(formatted[0], thousandsSep, grouping);
formatted[1] = formatted[1].replace(/0+$/, "");
return formatted[1] ? formatted.join(decimalPoint) : formatted[0];
}

patch(formatters, 'module_name.newFormatFloat', {
formatFloat(value, options = {}) {
const format_value = formatFloatNoTrailingZeros(value, options);
return format_value;
}
});

registry.category("formatters").remove('float');
registry.category("formatters").add("float", formatFloatNoTrailingZeros);

2. add assets to your __manifest__.py 

'assets': {
'web.assets_backend': [
'module_name/static/src/js/formatters.js',
]},

and then its work.

But in the end I gave up override the function, then patch function 

ListRendererPatch

its also worked, hope its useful to you

/** @odoo-module **/
import
{ patch } from "@web/core/utils/patch";
import
{ registry } from "@web/core/registry";
import
{ ListRenderer } from "@web/views/list/list_renderer";


const
formatters = registry.category("formatters");

patch(ListRenderer.prototype, 'module_name.ListRendererPatch', {
getFormattedValue(column, record) {
const fieldName = column.name;
const
field = this.fields[fieldName];
const
formatter = formatters.get(field.type, (val) => val);
const
formatOptions = {
escape: false,
data
: record.data,
isPassword
: "password" in column.rawAttrs,
digits
: column.rawAttrs.digits ? JSON.parse(column.rawAttrs.digits) : field.digits,
field
: record.fields[fieldName],
noTrailingZeros
: true,
};
//   options="{'keep_zero': True, 'set_digits': 5}" default noTrailingZeros is true
//  eg :
if
('set_digits' in column.options && formatOptions.digits && formatOptions.digits[1] !== undefined){
formatOptions.digits[1] = column.options.set_digits;
}
if ('keep_zero' in column.options){
formatOptions.noTrailingZeros = !column.options.keep_zero;
}
return formatter(record.data[fieldName], formatOptions);
}
});


Imagine profil
Abandonează
Cel mai bun răspuns

I have the same problem, have you solved this?

Imagine profil
Abandonează
Cel mai bun răspuns

To inherit and override this function in Odoo 16 OWL, you can use the following steps:

  1. Create a new module, for example, my_module.

  2. In your new module, create a file static/src/js/my_module.js and put the following code in it:

import {formatFloat as originalFormatFloat} from 'web/static/src/views/fields/formatters.js';

export function formatFloat(value, options = {}) { // Your custom code here // You can call the original function using originalFormatFloat() }

  1. In your new module, create a file views/assets.xml and put the following code in it:

  1. Install your new module in Odoo.

This will override the formatFloat function in your Odoo instance and use your custom implementation instead. You can modify the code in the my_module.js file to customize the behavior of the function as needed

Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
1
oct. 23
3164
2
dec. 22
2667
1
apr. 24
1880
0
ian. 24
1525
1
sept. 23
2927