Skip to content

Instantly share code, notes, and snippets.

@3mrdev
Last active June 11, 2022 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3mrdev/fd9191e3ab88e3bc4aa18783a9a0ee4c to your computer and use it in GitHub Desktop.
Save 3mrdev/fd9191e3ab88e3bc4aa18783a9a0ee4c to your computer and use it in GitHub Desktop.
Add a new button in top of the list view in Odoo when clicked it will call a method in your model
odoo.define('your_module.new_button', function (require) {
"use strict";
/**
* Button 'Create' is replaced by Custom Button
**/
var core = require('web.core');
var ListController = require('web.ListController');
ListController.include({
renderButtons: function($node) {
this._super.apply(this, arguments);
if (this.$buttons) {
this.$buttons.find('.oe_your_model_button').click(this.proxy('action_your_method'));
}
},
//--------------------------------------------------------------------------
// Define Handler for new Custom Button
//--------------------------------------------------------------------------
/**
* @private
* @param {MouseEvent} event
*/
action_your_method: function (e) {
var self = this;
var active_id = this.model.get(this.handle).getContext()['active_ids'];
var model_name = this.model.get(this.handle).getContext()['active_model'];
var searchQuery = this._controlPanel ? this._controlPanel.getSearchQuery() : {};
var record = self.model.get(self.handle, {raw: true});
var domain = record.getDomain();
this._rpc({
model: 'your_model',
method: 'your_method',
args: ["", model_name, active_id, domain, searchQuery],
}).then(function (result) {
self.do_action(result);
});
},
});
});
<?xml version="1.0" encoding="UTF-8"?>
<template xml:space="preserve">
<t t-extend="ListView.buttons">
<t t-jquery="button.o_list_export_xlsx" t-operation="before">
<button t-if="widget.modelName == 'your_model'" class="btn btn-primary oe_action_button oe_your_model_button" type="button" accesskey="f">Your Button</button>
</t>
</t>
</template>
from odoo import models, fields, api
class YourModel(models.Model):
_name = "your_model"
partner_id = fields.Many2one("res.partner")
def your_method(self, model_name, active_id, domain, searchQ):
recs = self.env["your_model"].search(domain)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment