Selectively display or hide button in the tree view in OpenERP

有些话、适合烂在心里 提交于 2019-12-25 04:09:07

问题


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:

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:


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>


来源:https://stackoverflow.com/questions/27553169/selectively-display-or-hide-button-in-the-tree-view-in-openerp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!