Skip to Content
Menu
This question has been flagged
4 Replies
14902 Views

Anyone please help me to change a button string dynamically(in tree view)..

I had used fields_view_get but when printing nodes, its empty.. my code is

@api.model
    def fields_view_get(self, view_id=None, view_type='tree', toolbar=False, submenu=False):
        res = super(ClassName, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
        doc = etree.XML(res['arch'])
        nodes = doc.xpath("//buttons"):
        print(nodes)
        return res

Avatar
Discard
Best Answer

At least in V13 (I didn't tried with older releases) you can put a field inside the button, i.e.

<button name="action_todo" icon="fa-edit" type="object"><field readonly="1" name="caption_field" options='{"no_open": True}'></button>

If the caption_field is a many2one field and  you omit the "no_open" option the button will work only clicking on the icon, clicking on the caption will open the related form model.    

The field could also be a computed field so the caption can also change dinamically



Avatar
Discard

I found it the best method, since we don't need to tamper in the '_get_view' method (works in Odoo 17)

Best Answer

Hi, 

I see that you have a ' at the end of buttons. 

In general, you need to specify the path to your button by specifying its name, .. for example : 
doc = etree.XML(res['arch'])

for node in doc.xpath("//button[@name='button_name']"):

        node.set('attrs',  "{'invisible': [.......]}")

        node.set('string',  "........")

        setup_modifiers(node, res['fields'])

 res['arch'] = etree.tostring(doc)


Upvote if this helps.

Regards.

Avatar
Discard
Best Answer

Change .text to .textContent to get/set the text content.

Or since you're dealing with a single text node, use .firstChild.data in the same manner.

Also, let's make sensible use of a variable, and enjoy some code reduction and eliminate redundant DOM selection by caching the result of getElementById.

function toggleText(button_id) 
{
   var el = document.getElementById(button_id);
   if (el.firstChild.data == "Lock") 
   {
       el.firstChild.data = "Unlock";
   }
   else 
   {
     el.firstChild.data = "Lock";
   }
}

Or even more compact like this:

function toggleText(button_id)  {
   var text = document.getElementById(button_id).firstChild;
   text.data = text.data == "Lock" ? "Unlock" : "Lock";
}

see also:
https://www.welookups.com
Avatar
Discard
Best Answer

The method Odoo uses is to have separate buttons and make them visible and invisible at the appropriate times (either using the "attrs" or "state" tags)

Take a look at how the Invoice buttons work:

<button name="action_invoice_sent" 
attrs="{'invisible':['|',('sent','=',True), ('state', 'not in', ('open','in_payment','paid'))]}"/>
<button name="148"
        attrs="{'invisible': [('state', '!=', 'open')]}"/>
<button name="action_invoice_open" states="draft" />
<button name="202"
attrs="{'invisible': ['|',('type', '=', 'out_refund'), ('state', 'not in', ('open','in_payment','paid'))]}"/>
<button name="action_invoice_draft" states="cancel"/>

                   

This may or may not be an option for you.

Avatar
Discard