ODOO Ninja

Quick tips for


Who are we?

Short Odoo tips and advices for programmers for both backend and frontend. Mainly written by Martin Fraňo. Slovak based programmer cooperating with Dutch company Bonebakker Computer Services specializing in Odoo development.


Trademarks

Odoo is trademark of Odoo S.A company.

Button next to form field

The first ever blog post is here. It might be a good idea to update this post with some more relevant content.




You can add button next to a form field with help of JavaScript widget.

Pozri field_button.js.

odoo.define('bantip_tools.field_button', function (require) {

    var AbstractField = require('web.AbstractField');
    var pyUtils = require('web.py_utils');

    AbstractField.include({

        _render: function () {
            var self = this;
            var ret = this._super.apply(this, arguments);
            if (this.__node && this.__node.children && this.getParent().viewType == 'form') {
                _.each(this.__node.children, function (child) {
                    if (child.tag === 'button') {
                        if (child.attrs && child.attrs.attrs) {
                            child.attrs.modifiers = typeof child.attrs.attrs == "string" ? pyUtils.py_eval(child.attrs.attrs) : child.attrs.attrs;
                        }
                        var btn = self.getParent()._renderTagButton(child);
                        if (btn) {
                            setTimeout(function () {
                                self.$el.parent().find('button[name="' + child.attrs.name + '"]').remove();
                                self.$el.wrap('
') .after(btn); btn.addClass('o_extra_button'); }); } } }); } return ret; }, updateModifiersValue: function(modifiers) { this._super.apply(this, arguments); if (this.$el.parent().hasClass('o_field_widget')) { if (modifiers.invisible) { this.$el.parent().addClass('o_invisible_modifier'); } else { this.$el.parent().removeClass('o_invisible_modifier'); } } } }); });

And some styles:

.o_with_button .o_field_widget {
    -webkit-box-flex: 1;
    -webkit-flex: 1 0 auto;
    flex: 1 0 auto;
}

.o_field_widget .o_extra_button {
    padding: 0;
    margin-left: 2px;
    font-size: 17px;
    color: #7C7BAD;
    border: none;
}

.o_field_widget.o_field_many2one .o_external_button:hover,
.o_field_widget .o_extra_button:hover {
    background-color: transparent;
    color: #0D0745;
}