Search code examples
javascriptxmlodooodoo-8

How to update field for multiple selection in TreeView of Odoo


I am trying to select multiple employees in tree view and want to update their status (selection field having two options i.e. Present, Absent).

status = fields.Selection(string="Status", selection=[('present', "Present"), ('absent', "Absent")])

My first approach:

I used act_window to create Present option in 'Actions' dropdown menu (which appears when multiple checkboxes are selected)

<act_window name="Present"
    res_model="hr.employee"
    src_model="hr.employee"
    multi="True"
    key2="client_action_multi"
    id="employee_present"
/>

And try to define a function for it in python file, but i can't link the act_window to that function.

My second approach:

Then I try another method of creating a button and linked it to a javascript file but also this approach can't solve my problem.

Below is the content of file hr_attendance/static/src/xml/qweb.xml

<?xml version="1.0" encoding="UTF-8"?>
<templates id="sync_template" xml:space="preserve">
    <t t-extend="ListView.buttons">
        <t t-jquery="button.o_list_button_add" t-operation="after">
            <button class="btn btn-sm btn-default sync_button" type="button">Present</button>
        </t>
    </t>
</templates>

Below is the content of sync.js hr_attendance/static/src/js

openerp.hr_attendance = function(instance) {
    var ListView = instance.web.ListView;
    console.log('Inside Javascript Code method call...');
    ListView.include({
        render_buttons: function() {
            // GET BUTTON REFERENCE
            this._super.apply(this, arguments)
            if (this.$buttons) {
                var btn = this.$buttons.find('.sync_button')
            }
            
            // PERFORM THE ACTION
            btn.on('click', this.proxy('do_sync'))
        },
        do_sync: function() {
            new instance.web.Model('hr.attendance')
            .call('present', [[]])
            .done(function() {
                alert('done')
            })
        }
    });
}

Below is the function which i am calling in javascript code

@api.multi
def present(self):
    self.write({'status': 'present'})

Solution

  • There is a nice community module called "Mass Editing" in the OCA Repositories. You can really easy create mass edit actions for business objects like employees and just activate them for the action menu ("More" in Odoo V8).

    Another solution is to create a server action with python code. Server actions can be activated for the action menu, too (there is a button in the right corner of a odoo V8 server action). But you will need two actions, where the first solution works with one action.

    The code for this server actions is really easy:

    obj.browse(env.context.get('active_ids')).write({'my_field': my_value})
    

    You obviously have to write on your selection field and the value for one server action is "Present" and "Absent" for the other.

    Server Actions can be found in Settings/technical/Actions/Server Actions.