Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
1241 Visualizzazioni

I'm running Odoo 17.0. I've created a custom model quote​ with a submodel quote.line_item​. (I know Odoo has its own quoting system, I'd like to write my own for practice.)

The ​tag for the line_item_ids​ field in the ​ view for the quote​ model has custom ​ links specified under ​ to add different types of line_item​s.

One of these ​ buttons autofills some defaults through the context​ attribute. This means, once the button is clicked, an auto-populated row appears.

When I click anywhere else on the page, the new row disappears. This appears to be some sort of default behaviour within Odoo, and I'd like to know how to override it. The row added by the button should be ready to save by default, even without user interaction.

Avatar
Abbandona
Autore Risposta migliore

After digging a bit more, I realized that this is mediated by the canBeAbandoned​​ getter within @web/model/record.Record​​, which is itself mediated by the Record.dirty​​ attribute.

I was able to achieve my desired behaviour by implementing a custom widget extending X2ManyField​​. I overrode onAdd​​ to set dirty​​ on the newly added record if an option in the context was set.

Here's the code: 

/** @odoo-module **/

import { _t } from "@web/core/l10n/translation";
import { registry } from "@web/core/registry";
import { makeContext } from "@web/core/context";
import {
    X2ManyField,
    x2ManyField,
} from "@web/views/fields/x2many/x2many_field";

export class LineItemX2ManyField extends X2ManyField {
    setup() {
        super.setup();
    }

    async onAdd({ context, editable } = {}) {
        context = makeContext([this.props.context, context]);

        // call the normal onAdd function, but if "forceDirty" is set, force the dirty
        // field to true so the record isn't immediately deleted upon no interaction.
        //'
        // we use this for records that are added with sensible defaults that may need
        // no tweaking, and can be left alone.
        return super.onAdd(...arguments).then((_) => {
            if (context.force_dirty) {
                this.list.records[this.list.records.length - 1].dirty = true;
            }
        });
    }
}

export const lineItemX2ManyField = {
    ...x2ManyField,
    component: LineItemX2ManyField,
};

registry.category("fields").add("line_item_x2_many", lineItemX2ManyField);
Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
0
ott 21
6122
4
dic 23
19943
2
apr 20
13177
2
apr 15
5527
1
mar 15
6892