4
votes

I am using odoo 10. I have a customer view and in that customer form view i am also showing all order that are associated with that specific customer in one2many tree view (editable).

What i want to do is i want to show a button in one2many tree view but i want to change that button's color only based on condition.

Here is what i tried but its not working.

<record id="amgl.customer_form" model="ir.ui.view">
        <field name="name">Customer</field>
        <field name="model">amgl.customer</field>
        <field name="arch" type="xml">
            <form string="Create New Customer" duplicate="0">
                <sheet>
                    <group colspan="4">
                        <group>
                            <field name="is_admin" invisible="1"/>
                            <field name="is_custodian" invisible="1"/>
                            <field name="is_vault" invisible="1"/>
                            <field name="is_o2m" invisible="1"/>
                            <field name="is_goldstar" invisible="1"/>
                            <field name="custodian_edit" invisible="1"/>
                            <field name="first_name"
                                   attrs="{'readonly':['|',('is_vault','=', True),('custodian_edit','=', True),('is_admin','=', False)]}"/>
                            <field name="last_name"
                                   attrs="{'readonly':['|',('is_vault','=', True),('custodian_edit','=', True),('is_admin','=', False)]}"/>
                            <field name="account_number"
                                   attrs="{'readonly':['|',('is_vault','=', True),('custodian_edit','=', True),('is_admin','=', False)]}"/>
                            <field name="gst_account_number" string="GoldStar Account Number"
                                   attrs="{'readonly':['|',('is_vault','=', True),('custodian_edit','=', True),('is_admin','=', False)], 'invisible':[('is_goldstar','=',False)]}"/>
                        </group>
                        <group>
                            <field name="date_opened"
                                   attrs="{'readonly':['|',('is_vault','=', True),('custodian_edit','=', True),('is_admin','=', False)]}"/>
                            <field name="account_type"
                                   attrs="{'readonly':['|',('is_vault','=', True),('custodian_edit','=', True),('is_admin','=', False)]}"/>
                            <field name="custodian_id" options='{"no_open": True}'
                                   attrs="{'readonly':[('is_admin','=', False)]}"/>
                            <field name="customer_notes"
                                   attrs="{'readonly':['|',('is_vault','=', True),('custodian_edit','=', True),('is_admin','=', False)]}"/>
                        </group>
                    </group>

                    <notebook>
                        <!--CURRENT INVENTORY-->
                        <page string="CURRENT INVENTORY">

                            <field name="customer_order_lines2"
                                   attrs="{'readonly':['|',('is_custodian','=', True),('is_o2m','=', False)]}"
                                   context="{'default_is_deposit_related': True,'group_by':'products'}"
                                   groups="amgl.group_amark_admins,amgl.group_amark_vault,amgl.group_amark_custodian,amgl.group_amark_sub_admins"
                                   domain="[('state','=','completed')]" default_order='state desc'
                                   widget="one2many_list">
                                <h4 style="float:right;">
                                    <field name="total_received_quantity"/>
                                </h4>
                                <tree open="false" editable="bottom"
                                      groups="amgl.group_amark_admins,amgl.group_amark_vault,amgl.group_amark_custodian,amgl.group_amark_sub_admins">
                                    <field name="is_deposit_related" invisible="1"/>

                                    <field name="is_vault_edit" invisible="1"/>
                                    <field name="is_admin" invisible="1"/>
                                    <field style="color:red !important;" name="products" attrs="{'readonly':[('is_vault_edit','=', True)]}"/>
                                    <field name="commodity"/>
                                    <field name="total_received_quantity"
                                           attrs="{'readonly':[('is_vault_edit','=', True)]}" class="oe_edit_only"
                                           col_border="1" string="Received"/>
                                    <field name="temp_received_weight" class="oe_edit_only" col_border="1"/>
                                    <field name="date_received" attrs="{'readonly':[('is_vault_edit','=', True)]}"
                                           class="oe_edit_only" col_border="1"/>
                                    <field name="state" invisible="1"/>
                                    <field name="notes_boolean" invisible="1"/>
                                    <button attrs="{'invisible':[('notes_boolean','=', False)]}" type="object" name="add_notes" class="btn btn-primary btn-sm o_list_button_add">
                                        <i class="fa-lg fa-pencil-square"></i>
                                    </button>
                                    <button attrs="{'invisible':[('notes_boolean','=', True)]}" type="object" name="add_notes" class="btn btn-default">
                                        <i class="fa-lg fa-pencil-square"></i>
                                    </button>
                                    <!--<button colors="red: notes_boolean is True" name="add_notes" type="object"-->
                                            <!--string="Add Notes" icon="fa-lg fa-pencil-square" class="btn btn-primary btn-sm o_list_button_addhlight"/>-->
                                </tree>
                            </field>
                        </page>
                    </notebook>
                </sheet>
            </form>
        </field>
    </record>
1

1 Answers

4
votes

What you are doing is the right solution you cannot change attributes of button using attrs so you need to create two button with the same name and label and show or hide one of them based on condition.

but this code will not work on tree because Odoo will not take your class attribute or style attribute in consideration no matter what you put there if you inspect the button element in your browser you will find that the two buttons are the same and the classes are the same and your classes are not there and there is no style attribute. I don't know why Odoo developer decided to do so.

I needed something like this before what I did is to add css in backend_assets that select a button in tree view with data-field attribute equal the name of the method because that attribute will not change in translation.

    /* make button color red in temporary exit tree view.*/
     .openerp .oe_list_content [data-field='cancel_entry'] button{
        background-color: red !important;
        color: white !important;
     }

you will notice that only this attribute change in the buttons that are in the tree view. so if you want to do something like this you need to to create two method

    @api.multi
    def add_notes_red(self): # i added _red to make sure no one else will name he's method like this
       pass

    @api.multi
    def add_notes_green(self):
       return self.add_notes_red() # because it does the same thing just call the orignal method

And in your custom css file

    /* make button color red.*/
     .openerp .oe_list_content [data-field='add_notes_red'] button{
        background-color: red !important;
        color: white !important;
     }

     /* make button color green.*/
     .openerp .oe_list_content [data-field='add_notes_green'] button{
        background-color: green !important;
        color: white !important;
     }

Note: Don't forget to css file in backend_assets template.

This is how it looks on my tree view Hope you like it.