dynamicly hide fields and rows in tree view odoo 9

2019-09-11 07:40发布

问题:

I have model to store training results for athletes and tree view to insert results from

model code is :

class GeneralFitnessDetails(Model):

    _name = 'general_fitness_details'
    generalFitnessDetails = Many2one("general_fitness")
    player = Many2one('player')
    exercise = Many2one("exercise")
    exercise_state = Selection([('by_reps', 'By Reps'),
                                ('by_time', 'By Time'),
                                ('by_distance', 'By Distance'),
                                ('by_weight', 'By Weight')])
    reps = Integer(string='Reps')
    time_sec = Integer(string='Seconds')
    weight = Integer(string='Weight/KG')
    distance = Integer(string='Distance/Meters')

here's view code

<tree>
<field name="player"/>
<field name="exercise"/>
<field name="exercise_state"/>
<field name="reps" invisible="[('exercise_state', '=', 'by_reps')]"/>
<field name="time_sec" invisible="[('exercise_state', '=', 'by_time')]"/>
<field name="weight" invisible="[('exercise_state', '=', 'by_weight')]"/>
<field name="distance" invisible="[('exercise_state', '=', 'by_distance')]"/>
</tree>

what I need is to create a button where it's function is : onClick , it filters the visibility of the shown columns in the tree view according to the exercise state ; so that if the exercise state's value is "by_distance" then it shows only the column that holds the values of by distance

Also the visibility have to be automatically changed so that if the exercise state becomes "by_weight" it changes to it and so on .

回答1:

You can not hide fields from tree view conditionally, even with attrs or direct with invisible attribute.

The reason is that tree view contains multiple records so it's not possible to hide field for 1 record and visible for others because headers are common for all the records. That's why invisible won't work conditionally in tree view.

Yes but it will work statically (not dynamically) as like,

invisible="1" or invisible="True"

You can use readonly attribute conditionally if tree view is editble.