0
votes

I have placed the buttons (icon is green arrow) in the treeview. I want to show a button only if the record has a Bill of Material (BOM). I do have logic how to find out that, but don't know how to selectively display or hide a button.

Also, the display or hide button action should be triggered when the view is loaded. How I can do that since there is no view_on_load event like in Visual Basic for instance?

Here is the tree view:

enter image description here

Here is how the buttons are defined in XML file:

<!-- mrp_bom -->
        <record id="adamson_mrp_bom_form_view" model="ir.ui.view">
            <field name="name">adamson.mrp.bom.form.view</field>
            <field name="model">mrp.bom</field>
            <field name="type">form</field>
            <field name="inherit_id" ref="mrp.mrp_bom_form_view" />
            <field name="arch" type="xml">
                <xpath expr="//notebook/page[@string='Components']/field/tree[@string='Components']/field[@name='sequence']" position="before" >
                     <button class="oe_inline oe_stat_button" type="object" string="Go!" icon="gtk-go-forward" name="action_go"  />

                </xpath>

Here is the logic how to find if there is a BOM or not for particular product. Note bom_ids list which most likely will have only one value. This logic is used for button action but it can be used for deciding to show button or not.

class mrp_bom_line(osv.osv):
    _inherit = 'mrp.bom.line'

    def action_go(self, cr, uid, ids, context=None):
        bom_obj = self.pool.get('mrp.bom')

        for bom_line in self.browse(cr, uid, ids, context=context):
            if bom_line.product_id.default_code > '300':
                bom_ids = bom_obj.search(cr, uid, [('product_id', '=', bom_line.product_id.id)], context=context)
                if bom_ids:
1

1 Answers

2
votes

You may use attrs for example

attrs="{'invisible':[('selection_field_name','=','value')]}"

Note: we need to give value which is store in database.

try this,

<xpath expr="//notebook/page[@string='Components']/field/tree[@string='Components']/field[@name='sequence']" position="before" >
    <button class="oe_inline oe_stat_button" type="object" string="Go!" icon="gtk-go-forward" name="action_go" attrs="{'invisible':[('type','=','normal')]}"  />
</xpath>