تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
4 الردود
15042 أدوات العرض

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

الصورة الرمزية
إهمال
أفضل إجابة

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



الصورة الرمزية
إهمال

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

أفضل إجابة

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.

الصورة الرمزية
إهمال
أفضل إجابة

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
الصورة الرمزية
إهمال
أفضل إجابة

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.

الصورة الرمزية
إهمال