Skip to Content
Menu
This question has been flagged
2 Replies
6911 Views

How can I make a  section and note like in sale order line in my new model have the samefields and idea of sale order line ?

Avatar
Discard
Best Answer

Add a Section /Add a note is just like a Widget, So you have to create one widget. Here is the sample JS code

...................................................................................................................................................................

 

odoo.define('sample_test.section_and_note_backend', function (require) {

// The goal of this file is to contain JS hacks related to allowing

// section and note on sale order and invoice.

// [UPDATED] now also allows configuring line items on the custom model.

"use strict";

var FieldChar = require('web.basic_fields').FieldChar;

var FieldOne2Many = require('web.relational_fields').FieldOne2Many;

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

var ListFieldText = require('web.basic_fields').ListFieldText;

var ListRenderer = require('web.ListRenderer');

var SectionAndNoteListRenderer = ListRenderer.extend({

    /**

     * We want section and note to take the whole line (except handle and trash)

     * to look better and to hide the unnecessary fields.

     *

     * @override

     */

    _renderBodyCell: function (record, node, index, options) {

        var $cell = this._super.apply(this, arguments);

        var isSection = record.data.display_type === 'line_section';

        var isNote = record.data.display_type === 'line_note';

        if (isSection || isNote) {

            if (node.attrs.widget === "handle") {

                return $cell;

            } else if (node.attrs.name === "name") {

                var nbrColumns = this._getNumberOfCols();

                if (this.handleField) {

                    nbrColumns--;

                }

                if (this.addTrashIcon) {

                    nbrColumns--;

                }

                $cell.attr('colspan', nbrColumns);

            } else {

                $cell.removeClass('o_invisible_modifier');

                return $cell.addClass('o_hidden');

            }

        }

        return $cell;

    },

    /**

     * We add the o_is_{display_type} class to allow custom behaviour both in JS and CSS.

     *

     * @override

     */

    _renderRow: function (record, index) {

        var $row = this._super.apply(this, arguments);

        if (record.data.display_type) {

            $row.addClass('o_is_' + record.data.display_type);

        }

        return $row;

    },

    /**

     * We want to add .o_section_and_note_list_view on the table to have stronger CSS.

     *

     * @override

     * @private

     */

    _renderView: function () {

        var self = this;

        return this._super.apply(this, arguments).then(function () {

            self.$('.o_list_table').addClass('o_section_and_note_list_view');

        });

    }

});

// We create a custom widget because this is the cleanest way to do it:

// to be sure this custom code will only impact selected fields having the widget

// and not applied to any other existing ListRenderer.

var SectionAndNoteFieldOne2Many = FieldOne2Many.extend({

    /**

     * We want to use our custom renderer for the list.

     *

     * @override

     */

    _getRenderer: function () {

        if (this.view.arch.tag === 'tree') {

            return SectionAndNoteListRenderer;

        }

        return this._super.apply(this, arguments);

    },

});

// This is a merge between a FieldText and a FieldChar.

// We want a FieldChar for section,

// and a FieldText for the rest (product and note).

var SectionAndNoteFieldText = function (parent, name, record, options) {

    var isSection = record.data.display_type === 'line_section';

    var Constructor = isSection ? FieldChar : ListFieldText;

    return new Constructor(parent, name, record, options);

};

fieldRegistry.add('sample_section_and_note_one2many', SectionAndNoteFieldOne2Many);

fieldRegistry.add('sample_section_and_note_text', SectionAndNoteFieldText);

return SectionAndNoteListRenderer;

});

--------------------------------------------------------------------------------------------------

Then you have to add it on your required  python  &  xml file, here the sample code

----------->Filename.py

 

display_type = fields.Selection([('line_section', "Section"),('line_note', "Note")], default=False, help="Technical field for UX purpose.")

 

--------------------------------------------------------------------------------------------------

 

On xml file, you have to add something more, please have a look into my comment..
Avatar
Discard

<field name="checklist_item_ids" mode="tree,kanban" widget=“sample_section_and_note_one2many">
<tree editable="bottom">
<control>
<create name="add_line_control" string="Add a line"/>
<create name="add_section_control" string="Add a section"
context="{'default_display_type': 'line_section'}"/>
</control>
<field name="sl_no" string="SL No"/>
<field name="name" widget=“sample_section_and_note_text" optional="show"/>
<field name="display_type" invisible="1"/>
</tree>
</field>

Best Answer

Hi,

You can add below code Inside a Tree Tag of  O2M field.

(Here Code  is  not  displaying  soi  I  mention  solution  in  comment )

Thanks 
Sunny Sheth

Avatar
Discard


<tree editable="bottom" string="Journal Items" >
<control>
<create name="add_line_control" string="Add a line"/>
<create name="add_section_control" string="Add a section" context="{'default_display_type': 'line_section'}"/>
<create name="add_note_control" string="Add a note" context="{'default_display_type': 'line_note'}"/>
</control>

<!-- Displayed fields -->
<field name="sequence" widget="handle"/>
<field name="move_name" invisible="1"/>
</tree>