Přejít na obsah
Odoo Menu
  • Přihlásit se
  • Vyzkoušejte zdarma
  • Aplikace
    Finance
    • Účetnictví
    • Fakturace
    • Výdaje
    • Spreadsheet (BI)
    • Dokumenty
    • Podpisy
    Prodej
    • CRM
    • Prodej
    • POS Obchod
    • POS Restaurace
    • Předplatné
    • Pronájem
    Webové stránky
    • Webové stránky
    • E-shop
    • Blog
    • Fórum
    • Živý chat
    • eLearning
    Dodavatelský řetězec
    • Sklad
    • Výroba
    • PLM
    • Nákup
    • Údržba
    • Kvalita
    Lidské zdroje
    • Zaměstnanci
    • Nábor
    • Volno
    • Hodnocení zaměstnanců
    • Doporučení
    • Vozový park
    Marketing
    • Marketing sociálních sítí
    • Emailový marketing
    • SMS Marketing
    • Události
    • Marketingová automatizace
    • Dotazníky
    Služby
    • Projekt
    • Časové výkazy
    • Práce v terénu
    • Helpdesk
    • Plánování
    • Schůzky
    Produktivita
    • Diskuze
    • Schvalování
    • IoT
    • VoIP
    • Znalosti
    • WhatsApp
    Aplikace třetích stran Odoo Studio Odoo cloudová platforma
  • Branže
    Maloobchod
    • Knihkupectví
    • Obchod s oblečením
    • Obchod s nábytkem
    • Potraviny
    • Obchod s hardwarem
    • Hračkářství
    Jídlo a pohostinství
    • Bar a Pub
    • Restaurace
    • Fast Food
    • Penzion
    • Distributor nápojů
    • Hotel
    Nemovitost
    • Realitní kancelář
    • Architektonická firma
    • Stavba
    • Správa nemovitostí
    • Zahradnictví
    • Asociace vlastníků nemovitosti
    Poradenství
    • Účetní firma
    • Odoo Partner
    • Marketingová agentura
    • Právník
    • Akvizice talentů
    • Audit a certifikace
    Výroba
    • Textile
    • Kov
    • Nábytek
    • Jídlo
    • Brewery
    • Korporátní dárky
    Zdraví a fitness
    • Sportovní klub
    • Prodejna brýli
    • Fitness Centrum
    • Wellness praktikové
    • Lékárna
    • Kadeřnictví
    Transakce
    • Údržbář
    • IT hardware a podpora
    • Systémy solární energie
    • Výrobce obuvi
    • Úklidové služby
    • Služby HVAC
    Ostatní
    • Nezisková organizace
    • Agentura pro životní prostředí
    • Pronájem billboardů
    • Fotografování
    • Leasing jízdních kol
    • Prodejce softwaru
    Browse all Industries
  • Komunita
    Edukační program
    • Tutoriály
    • Dokumentace
    • Certifikace
    • Vzdělávání
    • Blog
    • Podcast
    Podpora vzdělávání
    • Vzdělávací program
    • Scale Up! Hra na firmu
    • Navštivte Odoo
    Získat software
    • Stáhnout
    • Porovnejte edice
    • Verze
    Spolupráce
    • Github
    • Fórum
    • Události
    • Překlady
    • Stát se partnerem
    • Services for Partners
    • Registrujte svou účetní firmu
    Získat služby
    • Najít partnera
    • Najít účetní
    • Setkejte se s poradcem
    • Implementační služby
    • Zákaznické reference
    • Podpora
    • Upgrady
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dohodnout demo
  • Ceník
  • Pomoc

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

  • CRM
  • e-Commerce
  • Účetnictví
  • Sklad
  • PoS
  • Projekty
  • MRP
All apps
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
Pomoc

Odoo 16 Javascript overriding/inheritance

Odebírat

Get notified when there's activity on this post

This question has been flagged
javascriptinheritanceoverrideodoo16features
3 Odpovědi
12592 Zobrazení
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
Zrušit
Ahmad Zarour
Autor

Dear Ashish,

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

Avatar
PengXu
Nejlepší odpověď

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
Zrušit
Avatar
Nguyễn Ngọc Anh Khoa
Nejlepší odpověď

I have the same problem, have you solved this?

0
Avatar
Zrušit
Avatar
Ashish Hirpara
Nejlepší odpověď

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
Zrušit
Enjoying the discussion? Don't just read, join in!

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

Přihlásit se
Related Posts Odpovědi Zobrazení Aktivita
Odoo16 Owl Js Function override
javascript inheritance override OWL odoo16features
Avatar
Avatar
1
říj 23
4042
[v15] header: change selector for cart popup Vyřešeno
javascript inheritance override website_sale
Avatar
Avatar
2
pro 22
3467
Can't log the console inside the include javascript Odoo v16 Vyřešeno
javascript odoo16features
Avatar
Avatar
1
dub 24
3048
Odoo 16.0 CE - Missing widget: radio_reduce_selection for field of type selection
javascript odoo16features
Avatar
0
led 24
2564
What is the Best Practice for Integrating JavaScript in Odoo 16?
javascript odoo16features
Avatar
Avatar
1
zář 23
4147
Komunita
  • Tutoriály
  • Dokumentace
  • Fórum
Open Source
  • Stáhnout
  • Github
  • Runbot
  • Překlady
Služby
  • Odoo.sh hostování
  • Podpora
  • Upgrade
  • Nestandardní vývoj
  • Edukační program
  • Najít účetní
  • Najít partnera
  • Stát se partnerem
O nás
  • Naše společnost
  • Podklady značky
  • Kontakujte nás
  • Práce
  • Události
  • Podcast
  • Blog
  • Zákazníci
  • Právní dokumenty • Soukromí
  • Zabezpečení
الْعَرَبيّة 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 je balíček open-source aplikací, které pokrývají všechny potřeby vaší společnosti: CRM, e-shop, účetnictví, sklady, kasy, projektové řízení a další.

Unikátní nabídka od Odoo poskytuje velmi jednoduché uživatelské rozhraní a vše je integrované na jednom místě.

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