odooodoo-16

Error on creating One2many widget in Odoo 16


The idea is simple, I want to show all the data in one2many in an ordered list:

So simple. Should be so easy, but I have wasted 10 hours on it, still without a sign of success :(

Here is my js:

odoo.define('obki.accordion_widget', function (require) {
    'use strict';

    var core = require('web.core');
    var AbstractField = require('web.AbstractField');
    var field_registry = require('web.field_registry');
    var QWeb = core.qweb;

    var AccordionWidget = AbstractField.extend({
        // The template for the widget
        template: 'AccordionWidget',
        supportedFieldTypes: ['one2many'],
        // Render the widget
        _render: function () {
            var info = this.value;
            console.log('######### info: ', info);
            debugger; //it's not even executed
            this.$el.text('Accordion Widget Content'); // Display the Accordion name
        },
    });

    // Register the widget
    field_registry.add('accordion_widget', AccordionWidget);

    // return AccordionWidget;

    return {
        AccordionWidget: AccordionWidget,
    };
});

Here my XML:

<t t-name="AccordionWidget" owl="1">
    <ol>
    <t t-foreach="record.tac_ids" t-as="line">
        <li>
            <div><t t-esc="line.name"/></div>
        </li>
    </t>
    </ol>        
</t>

The result is always:

Missing widget: accordion_widget for field of type text

I have tried to copy another code from another 3rd_party plugin like this:

/** @odoo-module **/
import { registry } from '@web/core/registry';
import { X2ManyField } from '@web/views/fields/x2many/x2many_field';

export class Accordion extends X2ManyField {
}

Accordion.template = 'AccordionWidget';
registry.category('fields').add('accordion_widget', Accordion);

It seems working, the widget is registered and shown on the form. But unfortunately, I don't know how to access the data. How to read the field_ids data? I have tried this.field_ids, props.field_ids, props.record, props.everything, nothing works.


Solution

  • The one2many lines should be accessible using:

    this.props.value.records
    

    The error is raised from the new field registry, try the following code (first example upgraded):

    Component:

    /** @odoo-module **/
    
    import { registry } from "@web/core/registry";
    import { Component } from "@odoo/owl";
    
    const fieldRegistry = registry.category("fields");
    
    
    export class AccordionWidget extends Component {
    
    }
    
    AccordionWidget.template = "AccordionWidget"
    AccordionWidget.supportedTypes = ["one2many"];
    
    // Register the widget
    fieldRegistry.add('accordion_widget', AccordionWidget);
    

    Template:

    <t t-foreach="this.props.value.records" t-as="line" t-key="line_index">
       <li>
           <div><t t-esc="line.data.name"/></div>
       </li>
    </t>
    

    t-key attribute is required