Skip to Content
Odoo Menu
  • Log ind
  • Prøv gratis
  • Apps
    Økonomi
    • Bogføring
    • Fakturering
    • Udgifter
    • Regneark (BI)
    • Dokumenter
    • e-Signatur
    Salg
    • CRM
    • Salg
    • POS Butik
    • POS Restaurant
    • Abonnementer
    • Udlejning
    Hjemmeside
    • Hjemmesidebygger
    • e-Handel
    • Blog
    • Forum
    • LiveChat
    • e-Læring
    Forsyningskæde
    • Lagerbeholdning
    • Produktion
    • PLM
    • Indkøb
    • Vedligeholdelse
    • Kvalitet
    HR
    • Medarbejdere
    • Rekruttering
    • Fravær
    • Medarbejdersamtaler
    • Anbefalinger
    • Flåde
    Marketing
    • Markedsføring på sociale medier
    • E-mailmarketing
    • SMS-marketing
    • Arrangementer
    • Automatiseret marketing
    • Spørgeundersøgelser
    Tjenester
    • Projekt
    • Timesedler
    • Udkørende Service
    • Kundeservice
    • Planlægning
    • Aftaler
    Produktivitet
    • Dialog
    • Godkendelser
    • IoT
    • VoIP
    • Vidensdeling
    • WhatsApp
    Tredjepartsapps Odoo Studio Odoo Cloud-platform
  • Brancher
    Detailhandel
    • Boghandel
    • Tøjforretning
    • Møbelforretning
    • Dagligvarebutik
    • Byggemarked
    • Legetøjsforretning
    Mad og værtsskab
    • Bar og pub
    • Restaurant
    • Fastfood
    • Gæstehus
    • Drikkevareforhandler
    • Hotel
    Ejendom
    • Ejendomsmægler
    • Arkitektfirma
    • Byggeri
    • Ejendomsadministration
    • Havearbejde
    • Boligejerforening
    Rådgivning
    • Regnskabsfirma
    • Odoo-partner
    • Marketingbureau
    • Advokatfirma
    • Rekruttering
    • Audit & certificering
    Produktion
    • Tekstil
    • Metal
    • Møbler
    • Fødevareproduktion
    • Bryggeri
    • Firmagave
    Heldbred & Fitness
    • Sportsklub
    • Optiker
    • Fitnesscenter
    • Kosmetolog
    • Apotek
    • Frisør
    Håndværk
    • Handyman
    • IT-hardware og support
    • Solenergisystemer
    • Skomager
    • Rengøringsservicer
    • VVS- og ventilationsservice
    Andet
    • Nonprofitorganisation
    • Miljøagentur
    • Udlejning af billboards
    • Fotografi
    • Cykeludlejning
    • Softwareforhandler
    Gennemse alle brancher
  • Community
    Få mere at vide
    • Tutorials
    • Dokumentation
    • Certificeringer
    • Oplæring
    • Blog
    • Podcast
    Bliv klogere
    • Udannelselsesprogram
    • Scale Up!-virksomhedsspillet
    • Besøg Odoo
    Få softwaren
    • Download
    • Sammenlign versioner
    • Udgaver
    Samarbejde
    • Github
    • Forum
    • Arrangementer
    • Oversættelser
    • Bliv partner
    • Tjenester til partnere
    • Registrér dit regnskabsfirma
    Modtag tjenester
    • Find en partner
    • Find en bogholder
    • Kontakt en rådgiver
    • Implementeringstjenester
    • Kundereferencer
    • Support
    • Opgraderinger
    Github Youtube Twitter LinkedIn Instagram Facebook Spotify
    +1 (650) 691-3277
    Få en demo
  • Prissætning
  • Hjælp

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

  • CRM
  • e-Commerce
  • Bogføring
  • Lager
  • PoS
  • Projekt
  • MRP
All apps
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Hjælp

Odoo 16 Javascript overriding/inheritance

Tilmeld

Få besked, når der er aktivitet på dette indlæg

Dette spørgsmål er blevet anmeldt
javascriptinheritanceoverrideodoo16features
3 Besvarelser
12594 Visninger
Avatar
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
Avatar
Kassér
Ahmad Zarour
Forfatter

Dear Ashish,

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

Avatar
PengXu
Bedste svar

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
Avatar
Kassér
Avatar
Nguyễn Ngọc Anh Khoa
Bedste svar

I have the same problem, have you solved this?

0
Avatar
Kassér
Avatar
Ashish Hirpara
Bedste svar

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
Avatar
Kassér
Enjoying the discussion? Don't just read, join in!

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

Tilmeld dig
Related Posts Besvarelser Visninger Aktivitet
Odoo16 Owl Js Function override
javascript inheritance override OWL odoo16features
Avatar
Avatar
1
okt. 23
4042
[v15] header: change selector for cart popup Løst
javascript inheritance override website_sale
Avatar
Avatar
2
dec. 22
3468
Can't log the console inside the include javascript Odoo v16 Løst
javascript odoo16features
Avatar
Avatar
1
apr. 24
3049
Odoo 16.0 CE - Missing widget: radio_reduce_selection for field of type selection
javascript odoo16features
Avatar
0
jan. 24
2564
What is the Best Practice for Integrating JavaScript in Odoo 16?
javascript odoo16features
Avatar
Avatar
1
sep. 23
4156
Community
  • Tutorials
  • Dokumentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Oversættelser
Tjenester
  • Odoo.sh-hosting
  • Support
  • Opgradere
  • Individuelt tilpasset udvikling
  • Uddannelse
  • Find en bogholder
  • Find en partner
  • Bliv partner
Om os
  • Vores virksomhed
  • Brandaktiver
  • Kontakt os
  • Stillinger
  • Arrangementer
  • Podcast
  • Blog
  • Kunder
  • Juridiske dokumenter • Privatlivspolitik
  • Sikkerhedspolitik
الْعَرَبيّة 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 er en samling open source-forretningsapps, der dækker alle dine virksomhedsbehov – lige fra CRM, e-handel og bogføring til lagerstyring, POS, projektledelse og meget mere.

Det unikke ved Odoo er, at systemet både er brugervenligt og fuldt integreret.

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