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

Assign t-value in XML from JavaScript function

Tilmeld

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

Dette spørgsmål er blevet anmeldt
javascriptxmlt-value
2 Besvarelser
5125 Visninger
Avatar
Alberto Mastini

Hi everybody,


I'm trying to assign the t-value of a t element (in an xml file) from a javascript function.


Here is the Javascript function: 

function get_country_code()
{

$.getJSON('https://api.db-ip.com/v2/free/self', function(data)
{
    all_data = JSON.stringify(data, null, 2);
    var country_code = all_data.split(',')[3].split(':')[1];
    console.log(country_code);
  });

}


While this is how I'm trying to assign my t-value, but I tried in different ways as well (changing the this keyword to window for example):

t t-set="country" t-value="this.get_country_code()"


But I always get this error: 

Error while render the template
KeyError: 'this'
Template: website.layout

What is weird is that, when I'm debugging in the console, by typing "this.get_country_code()" my function gets executed correctly.


If you need more details just let me know! Any kind of suggestion is accepted! Thanks!


0
Avatar
Kassér
Avatar
Ashish Hirpara
Bedste svar

In Odoo, the this keyword only refers to the current object within the context of the Odoo environment. Therefore, you cannot use the this keyword to call a JavaScript function in your template. Instead, you can either call the function directly within your template using the t-esc attribute, or you can define a custom helper function in your template that calls your JavaScript function and returns the result.

Here is an example of how you can define a custom helper function in your template and call it using the t-esc attribute:

name="my_template">

Country code: t-esc="get_country_code()"/>

2
Avatar
Kassér
GUILLAUME LEBOURG

Hi Ashish, i don' t well understand your answer, could you give us a more detailled code please? THX a lot

Alberto Mastini
Forfatter

Hi, I just want to update you guys on this, I've found a workaround to get the country code without using javascript.
.
.
The following function is used to retrieve the country code:

def get_country_id(self):
# request the data from the url
url = 'http://ipinfo.io/json'
request = requests.get(url)
json_data = request.json()

if not odoo_request:
# get the country
country_code = json_data['country']
else:
country_code = odoo_request.geoip.get('country_code')
country_id = self.env['res.country'].search([
('code', '=', country_code)], limit=1)

return country_id
.
.
The only downside of it is that you can only test this in staging since to work it needs to be outside of localhost (at least this is what I've found out when I made this code).
.
.
If you want to test it locally, this is the code I've been using:

def get_country_id(self):
# request the data from the url
url = 'http://ipinfo.io/json'
request = requests.get(url)
json_data = request.json()

# TODO: don't push these modifies. needed only for local testing
# if not odoo_request:
# get the country
country_code = json_data['country']
# else:
# country_code = odoo_request.geoip.get('country_code')
country_id = self.env['res.country'].search([
('code', '=', country_code)], limit=1)

return country_id
.
.
Hope somebody can find this helpful!

Avatar
Alberto Mastini
Forfatter Bedste svar

Thanks for the answer! I will try it out and let you know.

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
Get data from JS file to XML file Løst
javascript xml
Avatar
Avatar
1
aug. 20
9869
Comparing javascript varriable with t-if
javascript xml
Avatar
Avatar
Avatar
2
jun. 20
4985
How to hide "section-financials" from the ProductInfoPopup in POS Screen Løst
javascript xml odoo16
Avatar
Avatar
2
jul. 24
1985
how to call javascript function on button with xml in odoo? Løst
javascript xml odoo10
Avatar
Avatar
2
jun. 23
43384
How to relate js variable with XML field in (odoo 15)
javascript xml barcode
Avatar
Avatar
1
dec. 22
4181
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