Search code examples
odooodoo-15

Odoo: is it possible to color Many2Many field by function?


i want to colorize this cells, module is "Calendar".

enter image description here

also, this place in views: enter image description here i can do this with adding

options='{"color_field":"color"}'

to field parameters, but it need to calculate color by checking, is attendee accepted or declined. If accepted, color - green, if decline - red. I can check this in field

attendee_ids = fields.One2many(
        'calendar.attendee', 'event_id', 'Participant')

also attendee model enter image description here How can i realize this?


Solution

  • You can define a new widget that inherits the many2manyattendee to color tags based on the attendee state field

    You will need to define the name of the color field and set its value for each attendee (tag)

    Example: (many2manyattendee.js)

    /** @odoo-module **/
    
    import fieldRegistry from 'web.field_registry';
    import relationalFields from 'web.relational_fields';
    
    
    const STATE_SELECTION = new Map([
        ['needsAction', 0],
        ['tentative', 2],
        ['declined', 1],
        ['accepted', 10]
    ]);
    
    const Many2ManyAttendeeColored = fieldRegistry.get("many2manyattendee").extend({
        _renderTags: function () {
            var self = this;
            var context = this._getRenderTagsContext();
            this.colorField = 'state_index';
            context.elements.forEach(function(item) {
                item[self.colorField] = STATE_SELECTION.get(self.record.specialData.partner_ids.find(partner => partner.id === item.id).status);
            });
            this._super.apply(this, arguments);
        },
    });
    
    fieldRegistry.add('many2many_attendee_colored', Many2ManyAttendeeColored);
    

    To load the JS code, add the following entry to the manifest file:

        'assets': {
            'web.assets_backend': [
                'MODULE_NAME/static/src/js/many2manyattendee.js',
    
            ],
        }
    

    To use it, change the widget on the partners' field to many2many_attendee_colored