Skip to Content
Odoo Meniu
  • Autentificare
  • Try it free
  • Aplicații
    Finanțe
    • Contabilitate
    • Facturare
    • Cheltuieli
    • Spreadsheet (BI)
    • Documente
    • Semn
    Vânzări
    • CRM
    • Vânzări
    • POS Shop
    • POS Restaurant
    • Abonamente
    • Închiriere
    Site-uri web
    • Constructor de site-uri
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Lanț Aprovizionare
    • Inventar
    • Producție
    • PLM
    • Achiziție
    • Maintenance
    • Calitate
    Resurse Umane
    • Angajați
    • Recrutare
    • Time Off
    • Evaluări
    • Referințe
    • Flotă
    Marketing
    • Social Marketing
    • Marketing prin email
    • SMS Marketing
    • Evenimente
    • Automatizare marketing
    • Sondaje
    Servicii
    • Proiect
    • Foi de pontaj
    • Servicii de teren
    • Centru de asistență
    • Planificare
    • Programări
    Productivitate
    • Discuss
    • Aprobări
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Aplicații Terțe Odoo Studio Platforma Odoo Cloud
  • Industrii
    Retail
    • Book Store
    • Magazin de îmbrăcăminte
    • Magazin de Mobilă
    • Magazin alimentar
    • Magazin de materiale de construcții
    • Magazin de jucării
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Distribuitor de băuturi
    • Hotel
    Proprietate imobiliara
    • Real Estate Agency
    • Firmă de Arhitectură
    • Construcție
    • Estate Managament
    • Grădinărit
    • Asociația Proprietarilor de Proprietăți
    Consultanta
    • Firma de Contabilitate
    • Partener Odoo
    • Agenție de marketing
    • Law firm
    • Atragere de talente
    • Audit & Certification
    Producție
    • Textil
    • Metal
    • Mobilier
    • Mâncare
    • Brewery
    • Cadouri corporate
    Health & Fitness
    • Club Sportiv
    • Magazin de ochelari
    • Centru de Fitness
    • Wellness Practitioners
    • Farmacie
    • Salon de coafură
    Trades
    • Handyman
    • IT Hardware and Support
    • Asigurare socială de stat
    • Cizmar
    • Servicii de curățenie
    • HVAC Services
    Altele
    • Organizație nonprofit
    • Agenție de Mediu
    • Închiriere panouri publicitare
    • Fotografie
    • Închiriere biciclete
    • Asigurare socială
    Browse all Industries
  • Comunitate
    Învăță
    • Tutorials
    • Documentație
    • Certificări
    • Instruire
    • Blog
    • Podcast
    Empower Education
    • Program Educațional
    • Scale Up! Business Game
    • Visit Odoo
    Obține Software-ul
    • Descărcare
    • Compară Edițiile
    • Lansări
    Colaborați
    • Github
    • Forum
    • Evenimente
    • Translations
    • Devino Partener
    • Services for Partners
    • Înregistrează-ți Firma de Contabilitate
    Obține Servicii
    • Găsește un Partener
    • Găsiți un contabil
    • Meet an advisor
    • Servicii de Implementare
    • Referințe ale clienților
    • Suport
    • Actualizări
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Obține un demo
  • Prețuri
  • Ajutor

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Contabilitate
  • Inventar
  • PoS
  • Proiect
  • MRP
All apps
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
All Posts Oameni Insigne
Etichete (View all)
odoo accounting v14 pos v15
Despre acest forum
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
All Posts Oameni Insigne
Etichete (View all)
odoo accounting v14 pos v15
Despre acest forum
Suport

Odoo 16 Javascript overriding/inheritance

Abonare

Primiți o notificare când există activitate la acestă postare

Această întrebare a fost marcată
javascriptinheritanceoverrideodoo16features
3 Răspunsuri
12616 Vizualizări
Imagine profil
Ahmad Zarour

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?

0
Imagine profil
Abandonează
Ahmad Zarour
Autor

Dear Ashish,

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

Imagine profil
PengXu
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);
}
});


0
Imagine profil
Abandonează
Imagine profil
Nguyễn Ngọc Anh Khoa
Cel mai bun răspuns

I have the same problem, have you solved this?

0
Imagine profil
Abandonează
Imagine profil
Ashish Hirpara
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

0
Imagine profil
Abandonează
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Înscrie-te
Related Posts Răspunsuri Vizualizări Activitate
Odoo16 Owl Js Function override
javascript inheritance override OWL odoo16features
Imagine profil
Imagine profil
1
oct. 23
4042
[v15] header: change selector for cart popup Rezolvat
javascript inheritance override website_sale
Imagine profil
Imagine profil
2
dec. 22
3480
Can't log the console inside the include javascript Odoo v16 Rezolvat
javascript odoo16features
Imagine profil
Imagine profil
1
apr. 24
3055
Odoo 16.0 CE - Missing widget: radio_reduce_selection for field of type selection
javascript odoo16features
Imagine profil
0
ian. 24
2573
What is the Best Practice for Integrating JavaScript in Odoo 16?
javascript odoo16features
Imagine profil
Imagine profil
1
sept. 23
4164
Comunitate
  • Tutorials
  • Documentație
  • Forum
Open Source
  • Descărcare
  • Github
  • Runbot
  • Translations
Servicii
  • Hosting Odoo.sh
  • Suport
  • Actualizare
  • Custom Developments
  • Educație
  • Găsiți un contabil
  • Găsește un Partener
  • Devino Partener
Despre Noi
  • Compania noastră
  • Active de marcă
  • Contactați-ne
  • Locuri de muncă
  • Evenimente
  • Podcast
  • Blog
  • Clienți
  • Aspecte juridice • Confidențialitate
  • Securitate
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo este o suită de aplicații de afaceri open source care acoperă toate nevoile companiei dvs.: CRM, comerț electronic, contabilitate, inventar, punct de vânzare, management de proiect etc.

Propunerea de valoare unică a Odoo este să fie în același timp foarte ușor de utilizat și complet integrat.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now