Search code examples
treeviewodoo-12

Add total label to tree footer odoo 12


I need to add a Total label on the last row from a tree, the last row is for sum values, how can I edit a tree footer?

view.xml

<xpath expr="//field[@name='field_list']/tree/field[@name='name']" position="after">
  <field name="field1" sum="Total Field 1"/>
  <field name="field2" sum="Total Field 2"/>
  <field name="field3" sum="Total Field 3"/>
</xpath>           

screenshot

enter image description here

Is there a way to edit the tree footer?


Solution

  • The list footer is used to show aggregates (sum, avg), it is rendered in _renderFooter of the ListRenderer.

    The method docstring:

    /**
         * Render the footer.  It is a <tfoot> with a single row, containing all
         * aggregates, if applicable.
         *
         * @private
         * @returns {jQueryElement} a <tfoot> element
         */
    

    The method that computes aggregates will check for the field type, the computation will be ignored if the field type is not one of the following integer, float or monetary.

    You can alter the _renderFooter to be able to provide a static text to display in the footer using a field attribute.

    Example:

    var ListRenderer = require("web.ListRenderer");
    ListRenderer.include({
            _renderFooter: function () {
                var res = this._super();
                _.each(this.columns, function (column) {
                   if(!('aggregate' in column) && column.attrs.text) {
                       res.find('.'+column.attrs.name).text(column.attrs.text);
                   }
                });
                return res;
            },
    });  
    

    To add the above code, check the Assets Management documentation.

    To display the text in the footer of the corresponding column, set the text attribute in the field tag ( if the field contain an aggregate, the text will be ignored).

    <field name="total" text="Total"/>