Skip to Content
Menu
This question has been flagged
3 Replies
5486 Views

Hi all,

How can I manage dynamic view by on_change function????

I have this view of a model:

<!-- Begening View dynamic.fields -->

<record id='dynamic_fields_tree' model='ir.ui.view'>

            <field name='name'>dynamic.fields.tree</field>
            <field name='model'>dynamic.fields</field>
            <field name='arch' type='xml'>
                <tree string='Dynamic Fields'>                    
                    <field name='name'/>
                    <field name='type_id'/>
                </tree>
            </field>
       </record>       
       <record id='dynamic_fields_form' model='ir.ui.view'>
            <field name='name'>dynamic.fields.form</field>
            <field name='model'>dynamic.fields</field>
            <field name='arch' type='xml'>
                <form string='Dynamic Fields' version="7.0">
                        <group>                   
                            <field name='date'/>
                            <field name='type_id' on_change='onchange_type_id(type_id)'/>
                            <field name='name' invisible='1'/>
                        </group>
                        <notebook>
                            <page string='Dynamic'>
                                <field name='answered' invisible='1'/>
                                <field name='lastname' invisible='1'/>
                                <field name='city' invisible='1'/>
                                <field name='married' invisible='1'/>
                            </page>
                        </notebook>
                </form>
            </field>
       </record>       
       <record id='dynamic_fields-act' model='ir.actions.act_window'>
            <field name='name'>Dynamic Fields</field>
            <field name='res_model'>dynamic.fields</field>
            <field name='view_type'>form</field>
            <field name='view_id' ref='dynamic_fields_tree'/>
            <field name='help' type='html'>
              <p class='oe_view_nocontent_create'>
                Create Dynamic Fields. 
              </p>
            </field>
        </record>        
        <menuitem 
            action='dynamic_fields-act' 
            name='Dynamic Fields' 
            id='menu_dynamic_fields-act' 
            parent='base.menu_sales'
            sequence='40'/>

<!-- End view dynamic.fields -->

And in the function "onchange_type_id", I have:

def onchange_type_id(self, cr, uid, ids, type_id, context=None):
        if context is None:
            context = {}
        res = super(test_dynamic_fileds,self).fields_view_get(cr, uid, view_id=None, view_type='form', context=context, toolbar=False, submenu=False)
        all_fields = []
        dyn_op_fields_obj = self.pool.get('dynamic.operation.champs')
        type_op_fields = dyn_op_fields_obj.search(cr, uid, [('operation_id', '=', type_id)])
        for t in dyn_op_fields_obj.browse(cr, uid, type_op_fields):
            all_fields.append(t.name)
        
        arch = etree.XML(res['arch'])
        for f in all_fields:
            for node in arch.xpath("//field[@name='" + f + "']"):
                node.set('invisible', '0')
        
        res['arch'] = etree.tostring(arch)

return res

I want for a given type, the view is different.
Ex: for the type 1, the field name, city, married are visible and not visible to the type 2.

Thx,

Avatar
Discard
Best Answer

Hi  Bernscheinder,

I think you can achieve your goal in the example by using the attrs tag as follows


 <page string='Dynamic'>
 ....
<field name='married' attrs="{'invisible':['type','=',2)]}"/>
</page>


Avatar
Discard
Author Best Answer

Thx, i've resolved it!!

Avatar
Discard
Related Posts Replies Views Activity
0
May 16
2816
1
Oct 15
6462
1
Mar 15
8352
5
Aug 24
42978
2
Apr 24
949