Search code examples
javascriptxmlodooodoo-12

Odoo 12 Create button on Tree View on stock.quant


Is there any way to create a button on stock.quant model?

All solution i found is related to Create/Import Buttons, BUT on stock.quant model doesn't have them.

My code so far:

Path: locations_quants_report/views/templates.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
      <data>
            <template id="assets_backend" name="stock_quant_tree_view_button" inherit_id="web.assets_backend">
                  <xpath expr="." position="inside">
                        <script type="text/javascript" src="/tree_menu/static/src/js/tree_view_button.js"></script>
                  </xpath>
            </template>
      </data>
</odoo>

Path: locations_quants_report/static/src/xml/tree_view_button.xml

<?xml version="1.0" encoding="UTF-8"?>
      <template id="template" xml:space="preserve">
          <t t-extend="ListView.buttons">
            <t t-jquery="button.o_list_button_add" t-operation="after">
                  <button t-if="widget.model == 'stock.quant'" class="btn btn-primary" type="button">Print</button>
            </t>
          </t>
      </template>

Also declare them on __manifest__.py

    'data': [
        ...
        'views/template.xml'
    ],
    'qweb': [
        'static/src/xml/tree_view_button.xml',
    ],

Any solution i whould be great. Thanks in advance!


Solution

  • I found solution to call the wizard i made for printing the lines.

    Path: locations_quants_report/views/templates.xml

    <?xml version="1.0" encoding="utf-8"?>
    <odoo>
        <data>
            <template id="assets_backend"  name="stock_quant_tree_view_button" inherit_id="web.assets_backend">
                <xpath expr="." position="inside">
                    <script type="text/javascript" src="/locations_quants_report/static/src/js/tree_view_button.js"></script>
                </xpath>
            </template>
        </data>
    </odoo>
    

    Path: locations_quants_report/static/src/xml/tree_view_button.xml

    <?xml version="1.0" encoding="UTF-8"?>
          <template id="template" xml:space="preserve">
              <t t-extend="ListView.buttons">
                <t t-jquery="button.o_list_button_save" t-operation="before">
                    <t t-if="widget.modelName == 'stock.quant'">
                      <button id="custom_print_btn" class="btn btn-primary o_list_button_custom_print" type="button" >Print</button> 
                    </t>
                </t>
              </t>
          </template>
    

    Path: locations_quants_report/static/src/js/tree_view_button.js

    odoo.define('locations_quants_report.tree_view_button', function (require){
        "use strict";       
        var core = require('web.core');
        var ListView = require('web.ListView'); 
        var ListController = require("web.ListController");
    
        var includeDict = {
            renderButtons: function () {
                this._super.apply(this, arguments);
                if (this.modelName == 'stock.quant') {
                    var your_btn = this.$buttons.find('button.o_list_button_custom_print');
                    your_btn.on('click', this.proxy('o_list_button_custom_print'));
                }
            },
            o_list_button_custom_print: function(){
                this.do_action({
                    name: "Open a wizard",
                    type: 'ir.actions.act_window',
                    res_model: 'locations.quants.report',
                    view_mode: 'form',
                    view_type: 'form',
                    views: [[false, 'form']],
                    target: 'new',
                });
            }
        };
    
        ListController.include(includeDict);
    });
    

    Also declare them on __manifest__.py

        'data': [
            ...
            'views/template.xml'
        ],
        'qweb': [
            'static/src/xml/tree_view_button.xml',
        ],