Overslaan naar inhoud
Odoo Menu
  • Aanmelden
  • Probeer het gratis
  • Apps
    Financiën
    • Boekhouding
    • Facturatie
    • Onkosten
    • Spreadsheet (BI)
    • Documenten
    • Ondertekenen
    Verkoop
    • CRM
    • Verkoop
    • Kassasysteem winkel
    • Kassasysteem Restaurant
    • Abonnementen
    • Verhuur
    Websites
    • Websitebouwer
    • E-commerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Bevoorradingsketen
    • Voorraad
    • Productie
    • PLM
    • Inkoop
    • Onderhoud
    • Kwaliteit
    Personeelsbeheer
    • Werknemers
    • Werving & Selectie
    • Verlof
    • Evaluaties
    • Aanbevelingen
    • Wagenpark
    Marketing
    • Social media Marketing
    • E-mailmarketing
    • SMS Marketing
    • Evenementen
    • Marketingautomatisering
    • Enquêtes
    Diensten
    • Project
    • Urenstaten
    • Buitendienst
    • Helpdesk
    • Planning
    • Afspraken
    Productiviteit
    • Chat
    • Goedkeuringen
    • IoT
    • VoIP
    • Kennis
    • WhatsApp
    Apps van derden Odoo Studio Odoo Cloud Platform
  • Bedrijfstakken
    Detailhandel
    • Boekhandel
    • kledingwinkel
    • Meubelzaak
    • Supermarkt
    • Bouwmarkt
    • Speelgoedwinkel
    Food & Hospitality
    • Bar en Pub
    • Restaurant
    • Fastfood
    • Gastenverblijf
    • Drankenhandelaar
    • Hotel
    Vastgoed
    • Makelaarskantoor
    • Architectenbureau
    • Bouw
    • Vastgoedbeheer
    • Tuinieren
    • Vereniging van eigenaren
    Consulting
    • Accountantskantoor
    • Odoo Partner
    • Marketingbureau
    • Advocatenkantoor
    • Talentenwerving
    • Audit & Certificering
    Productie
    • Textiel
    • Metaal
    • Meubels
    • Eten
    • Brewery
    • Relatiegeschenken
    Gezondheid & Fitness
    • Sportclub
    • Opticien
    • Fitnesscentrum
    • Wellness-medewerkers
    • Apotheek
    • Kapper
    Trades
    • Klusjesman
    • IT-hardware & support
    • Zonne-energiesystemen
    • Schoenmaker
    • Schoonmaakdiensten
    • HVAC-diensten
    Andere
    • Non-profitorganisatie
    • Milieuagentschap
    • Verhuur van Billboards
    • Fotograaf
    • Fietsleasing
    • Softwareverkoper
    Browse all Industries
  • Community
    Leren
    • Tutorials
    • Documentatie
    • Certificeringen
    • Training
    • Blog
    • Podcast
    Versterk het onderwijs
    • Onderwijs- programma
    • Scale Up! Business Game
    • Bezoek Odoo
    Download de Software
    • Downloaden
    • Vergelijk edities
    • Releases
    Werk samen
    • Github
    • Forum
    • Evenementen
    • Vertalingen
    • Word een Partner
    • Services for Partners
    • Registreer je accountantskantoor
    Diensten
    • Vind een partner
    • Vind een boekhouder
    • Een adviseur ontmoeten
    • Implementatiediensten
    • Klantreferenties
    • Ondersteuning
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Vraag een demo aan
  • Prijzen
  • Help

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

  • CRM
  • e-Commerce
  • Boekhouding
  • Voorraad
  • PoS
  • Project
  • MRP
All apps
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Help

Show or hide a server action button based on a field value on odoo V17 CE

Inschrijven

Ontvang een bericht wanneer er activiteit is op deze post

Deze vraag is gerapporteerd
actiondevelopementvisibilityserver_actions17.0
3 Antwoorden
2174 Weergaven
Avatar
Master

Hi,

How can I show or hide a server action button based on a field value in my model?

For example, if the state field is 'closed', I want the "Open" button to be visible.

And if the state is 'open', I want the "Close" button to be displayed instead.

Is there a way to conditionally show or hide these server actions depending on the record's state?


<record id="mymodel_open_action_server" model="ir.actions.server">

<field name="name">Open mymodel</field>

<field name="model_id" ref="model_mymodel"/>

<field name="binding_model_id" ref="model_mymodel"/>

<field name="binding_view_types">form</field>

<field name="state">code</field>

<field name="code">

action = records.open_action()

</field>

0
Avatar
Annuleer
Avatar
Cybrosys Techno Solutions Pvt.Ltd
Beste antwoord

Hi,

Please refer to the solved forum:

1. https://www.odoo.com/id_ID/forum/help-1/how-to-hide-action-button-based-on-condition-246523

2. https://www.odoo.com/id_ID/forum/help-1/hide-a-menu-inside-action-button-based-on-condition-168198


Hope it helps.

0
Avatar
Annuleer
Avatar
slckatav
Beste antwoord

Hey, could you share your open_action() function as well? Since you’d prefer not to use a button seeing that code will help me suggest the right approach. In general you can add an attrs to your XML to toggle visibility. For example:

attrs="{'invisible': [('state', 'in', ['sale'])]}"

This will hide the element when state is not in the “sale” stage. Of course, you can adjust the domain or use different attribute just a quick example to get you started.

0
Avatar
Annuleer
Master
Auteur

Hi, you can see my code below for more details:

<!-- Server Action -->
<record id="mymodel_closed_action_server" model="ir.actions.server">
<field name="name">Close</field>
<field name="model_id" ref="model_mymodel"/>
<field name="binding_model_id" ref="model_mymodel"/>
<field name="state">code</field>
<field name="code">action = records._action_close()</field>
</record>

def _action_close(self):
for rec in self:
if rec.state != "closed":
print("My logic goes here!")
rec.state = "closed"

slckatav

I once hid some action buttons by finding their generated names. Odoo assigns its own names to action buttons like button name="356". In your case, after you add your action button find the name that Odoo generated and then add XML like this:

<xpath expr="//button[@name='466']" position="attributes">
<attribute name="invisible">True</attribute>
</xpath>

I hope this helps!

Avatar
D Enterprise
Beste antwoord

Hiii,

To show/hide buttons based on the state field in Odoo 17 Community Edition , you must use button elements inside the form view , not ir.actions.server bindings, because ir.actions.server buttons do not support dynamic visibility using attrs .

Here's the correct and clean implementation :

Model: mymodel.py

Define state , action_open , and action_close :

from odoo import models, fields class MyModel (models.Model): _name = 'mymodel' _description = 'My Model' state = fields.Selection([ ( 'open' , 'Open' ), ( 'closed' , 'Closed' ) ], default= 'open' , string= 'State' ) def action_open ( self ): for record in self: record.state = 'open' def action_close ( self ): for record in self: record.state = 'closed'

View: mymodel_form_view.xml

Inherit or define your form view and add two buttons, conditionally shown using attrs .

<record id ="view_form_mymodel" model="ir.ui.view"> <field name = "name" >mymodel.form </field> <field name = "model" >mymodel </field> <field name = "arch" type = "xml"> <form string = "My Model"> <header> <button name="action_open"

​ ​ ​ ​ ​        type="object"

​ ​ ​ ​​        string="Open"

        ​ ​ ​ ​ ​invisible="state != 'closed'"/> <button name="action_close"

​ ​ ​ ​ ​        type="object"

​ ​ ​ ​ ​        string="Close"

​ ​ ​ ​ ​        invisible="state != 'open'"/> </header> <sheet> <group> <field name = "state"/> <!-- other fields --> </group> </sheet> </form > </field > </record>

i hope it is use full

0
Avatar
Annuleer
Master
Auteur

This works with a normal <button> tag. However, in my case, I need to show or hide my custom choices Close/Open in the default server action, similar to the behavior of the Archive/Unarchive button in

D Enterprise

Use the binding_filter_domain field (Best Practice)
From Odoo 15+, ir.actions.server supports a field called binding_filter_domain, which defines when a server action appears in the action dropdown.

Example: Show “Open” action only if state == 'closed'
<record id="mymodel_open_action_server" model="ir.actions.server">
<field name="name">Open</field>
<field name="model_id" ref="model_mymodel"/>
<field name="binding_model_id" ref="model_mymodel"/>
<field name="binding_view_types">form</field>
<field name="binding_filter_domain">[('state', '=', 'closed')]</field>
<field name="state">code</field>
<field name="code">
action = records.action_open()
</field>
</record>
Example: Show “Close” action only if state == 'open'
<record id="mymodel_close_action_server" model="ir.actions.server">
<field name="name">Close</field>
<field name="model_id" ref="model_mymodel"/>
<field name="binding_model_id" ref="model_mymodel"/>
<field name="binding_view_types">form</field>
<field name="binding_filter_domain">[('state', '=', 'open')]</field>
<field name="state">code</field>
<field name="code">
action = records.action_close()
</field>
</record>
please try this
i hoe it is use full

Geniet je van het gesprek? Blijf niet alleen lezen, doe ook mee!

Maak vandaag nog een account aan om te profiteren van exclusieve functies en deel uit te maken van onze geweldige community!

Aanmelden
Gerelateerde posts Antwoorden Weergaven Activiteit
¿how to create planned actions? Opgelost
action actions server_actions
Avatar
Avatar
2
mrt. 23
3273
(!!copair~rápido!!) Cómo puedo hablar con una persona real en Copa?
action
Avatar
0
nov. 25
4
Notification Email Alias – What Emails Are Sent?
action
Avatar
0
nov. 25
5
odoo 19, Enterprise version odoo on - premise.
action
Avatar
Avatar
2
nov. 25
134
in POS orderline is it possible to add background color to the discounted product line
17.0
Avatar
Avatar
1
nov. 25
72
Community
  • Tutorials
  • Documentatie
  • Forum
Open Source
  • Downloaden
  • Github
  • Runbot
  • Vertalingen
Diensten
  • Odoo.sh Hosting
  • Ondersteuning
  • Upgrade
  • Gepersonaliseerde ontwikkelingen
  • Onderwijs
  • Vind een boekhouder
  • Vind een partner
  • Word een Partner
Over ons
  • Ons bedrijf
  • Merkelementen
  • Neem contact met ons op
  • Vacatures
  • Evenementen
  • Podcast
  • Blog
  • Klanten
  • Juridisch • Privacy
  • Beveiliging
الْعَرَبيّة 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 is een suite van open source zakelijke apps die aan al je bedrijfsbehoeften voldoet: CRM, E-commerce, boekhouding, inventaris, kassasysteem, projectbeheer, enz.

Odoo's unieke waardepropositie is om tegelijkertijd zeer gebruiksvriendelijk en volledig geïntegreerd te zijn.

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