Search code examples
javascriptpythonodoorpc

odoo 14 get selected recored is tree view JS


I want to get the ids of the selected recored from a tree view

What is did is add a button to the view which when clicked I get the self.selectedRecords in my python function test_func but I am not getting the

But I am getting ['sale.order_33', 'sale.order_40', 'sale.order_48']

instead of ['sale.order_19', 'sale.order_18', 'sale.order_2']

enter image description here


ListController.include({
        renderButtons: function($node) {
            this._super.apply(this, arguments);
            var self = this;
            if (this.$buttons) {
                $(this.$buttons).find('.oe_new_custom_button').on('click', function() {
                    rpc.query({
                        model: 'sale.order',
                        method: 'test_func',
                        args: [self.selectedRecords],
                    }).then(function(res){
                        console.log(self.selectedRecords);
                        console.log(res);
                    });
                });
            }
        },
    });

class test_func_report(models.Model):
    _inherit = 'sale.order'
        
    @api.model
    def test_func(self,selectedRecords):
        _logger.warning(self)
        _logger.warning(selectedRecords)


Solution

  • Use the getSelectedIds() method to get the currently selected records ids.

    Use self.getSelectedIds() instead of self.selectedRecords.