This question has been flagged

Hi, I have form view with many2many field account_ids with its records presented in tree view.
Inside tree view, there is field and button. I want to click on button and copy value of child_name to parent_name. It should works like copy and paste or like default parameter in modules. No saving. 


<form>
    <sheet>
        <field name="parent_name"/>                      
        <notebook>
            <page>                
                <field name="account_ids">
                    <tree>
                        <field name="child_name"/>
                        <button icon="fa-upload" name="button"/>              
                    </tree>
                </field>                     
            </page>
        </notebook>            
    </sheet>
</form>


The problem is that clicking on a button always causes the client to issue a write or create call before running the method.

So, I think only way to do that is to use Javascript/jQuery.


odoo.define('budget.button.copy_name'function (require) {
    "use strict";

    var FieldRegistry = require('web.field_registry');

    // extend web.relational_fields???
    var FieldMany2Many = require('web.relational_fields').FieldMany2Many;
    
    // extend web.FormController???
    var FormController = require('web.FormController'); 
    
    // extend web.ListController
    var FormController = require('web.ListController'); 

    // what JS/jQuery code to put here? something like this?
    var CopyFieldMany2Many = FieldMany2Many.extend({
        _onButtonClickedfunction (ev) {
            // what to put here?
        }
    });

    FieldRegistry.add('copy_name'CopyFieldMany2Many);
});


How should view look like? Is...

                        <button icon="fa-upload" name="button"/>              

...good?

Or it should be like this?

                        <field name="account_ids" widget="copy_name">


Or maybe should I do this in OWL Framework?

Big thanks for helping me :)






 



Avatar
Discard
Author Best Answer

With the help of my brother we've managed to do that. This is the solution:

Changes in XML:

<form string="Budget Line" js_class="copy_name">
...
<button name="button" button_class="button_copy_name" icon="fa-upload">

Whole JS file:

odoo.define('budget.button.copy_name', function (require) {
    "use strict";
    var FormController = require('web.FormController');
    var FormView = require('web.FormView');
    var viewRegistry = require('web.view_registry');
    var CopyFormController = FormController.extend({}, {
        _onButtonClicked: function (ev) {
            if(ev.data.attrs.button_class === 'button_copy_name') {
                // put concatenated strings into input
                // without change() you won't save inputed value
                // focus() is to put cursor into input field (i.e. to edit text immediately)             
                $("input[name='name']").val(`${ev.data.record.data.code} ${ev.data.record.data.name}`).change().focus(); 
            } else {
                return this._super.apply(this, arguments)
            }            
        }
    });
    var CopyFormView = FormView.extend({
        config: _.extend({}, FormView.prototype.config, {
            Controller: CopyFormController,
        }),
    });
    viewRegistry.add('copy_name', CopyFormView);
});








Avatar
Discard