Skip to Content
Menu
This question has been flagged
4 Replies
13358 Views

When viewing a form in read mode (not editing the form), the user can click on a many2one field, to quickly jump to the form of this record.

The same thing I wish for the many2many_tags widget. I want that when viewing the form and clicking on one of the many2many tags, the corresponding record should be opened. At the moment nothing happens. I think this would be useful and I don't see any down side.

So is it possible to modify the many2many_tags widget to achieve that? What would I have to do?

Avatar
Discard
Best Answer

Try this and call the widget as 'my_many2many_tags' or change the name:

odoo.define('my_module.my_widget', function (require) { 
"use strict";
var core = require('web.core');
var common = require('web.form_common');
var FieldMany2ManyTags = core.form_widget_registry.get('many2many_tags');
var _t = core._t;
    var MyFieldMany2ManyTags = FieldMany2ManyTags.extend({ 
get_badge_id: function(el){
if ($(el).hasClass('badge')) return $(el).data('id');
return $(el).closest('.badge').data('id');
},
events: {
'click .o_delete': function(e) {
e.stopPropagation();
this.remove_id(this.get_badge_id(e.target));
},
'click .badge': function(e) {
e.stopPropagation();
var self = this;
var record_id = this.get_badge_id(e.target);
new common.FormViewDialog(self, {
res_model: self.many2one.field.relation,
res_id: record_id,
context: self.dataset.context,
title: _t('Open: ') + self.many2one.string,
readonly: self.many2one.get('effective_readonly')
}).on('write_completed', self, function() {
self.dataset.cache[record_id].from_read = {};
self.dataset.evict_record(record_id);
self.render_value();
}).open();
}
}
});
    core.form_widget_registry.add('my_many2many_tags', MyFieldMany2ManyTags); 

return {
        MyFieldMany2ManyTags: MyFieldMany2ManyTags 
};
});

Avatar
Discard

Hello pablo, I want to achieve the same thing but instead of open form view in dialog, I want to open the form in the current form view. can you please help.

Best Answer

Heres a version of Pablo's answer that works in Odoo 13. Cheers pablo for the template!

odoo.define('my_module.my_widgets', function (require) {
"use strict";

var core = require('web.core');
var dialogs = require('web.view_dialogs');
var registry = require('web.field_registry');
var rel_fields = require('web.relational_fields');
var _t = core._t;

var FieldMany2ManyTagLinks = rel_fields.FieldMany2ManyTags.extend({
get_badge_id: function (el) {
if ($(el).hasClass('badge')) return $(el).data('id');
return $(el).closest('.badge').data('id');
},
events: _.extend({}, rel_fields.FieldMany2ManyTags.prototype.events, {
'click .badge': function (e) {
e.stopPropagation();
var self = this;
var record_id = this.get_badge_id(e.target);
new dialogs.FormViewDialog(self, {
res_model: self.field.relation,
res_id: record_id,
context: self.record.getContext(),
title: _t('Open: ') + self.field.string,
readonly: !self.attrs.can_write,
}).on('write_completed', self, function () {
self.dataset.cache[record_id].from_read = {};
self.dataset.evict_record(record_id);
self.render_value();
}).open();
}
})
});
registry.add('many2many_taglinks', FieldMany2ManyTagLinks);

return {
FieldMany2ManyTagLinks: FieldMany2ManyTagLinks
};

});

Avatar
Discard

Grrr odoo forum formatting! Will fix for the next one :)

Bro, you saved my ass XD, it oso works in Odoo15

Best Answer

Sorry for bother you but how did you made it works? can you show me the structure of the module please?


Avatar
Discard
Best Answer

i have the same problem but this is the first time that i use JS . can you help me resolve this problem . i need the steps and thanks 

Avatar
Discard
Related Posts Replies Views Activity
2
Sep 23
7996
1
May 19
6321
3
Oct 18
19430
1
Nov 18
6362
3
Jan 20
6948