Skip to content Skip to sidebar Skip to footer

Odoo10 Javascript Widget Action Not Working: ClientWidget Is Not A Function

I'm still learning how to properly add your own javascript code. I'm trying to add my own stuff into Odoo backend. I was carefully reading 'Building Interface Extensions' guide (ht

Solution 1:

I'm also new to Odoo and JS, and I have tried something and that is given below:

    In your /static/src/js/your_.js file, add the code:

    odoo.define('zbtoken.map', function (require) {
        "use strict";

    var Widget = require('web.Widget');
    var core = require('web.core');
    var utils = require('web.utils');


    var HomePage = Widget.extend({
       template: "HelloJS",

       init: function(parent) {
            this._super(parent);
            console.log("Hello JS, I'm inside of init.");
       },


    start: function() {
            console.log("Hello JS, I'm inside of start.");
     },
   });

   core.action_registry.add('HelloJS', HomePage);

   return HomePage;
   }); 

In your /static/src/xml/your_.xml file:

    <?xml version="1.0"  encoding="UTF-8"?>
    <templates xml:space="preserve">
       <t t-name="HelloJS">
          <div> </div>
       </t>
    </templates>

In your /views/your_.xml file:

   <?xml version="1.0" encoding="UTF-8"?>
    <odoo>
      <data>

    <template id="assets_backend" name="petstore" 
        inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script type="text/javascript"         
            src="/your_module_name/static/src/js/your_js_file.js">
            </script>
        </xpath>
    </template> 

    <record id="hellojs_id" model="ir.actions.client">
        <field name="name">HelloJS</field>
        <field name="tag">HelloJS</field>
    </record>
     <menuitem id="hellojs_menu" 
            name="HelloJS" 
            action="hellojs_id"/>
     </data>
     </odoo>

In manifest.py:

     'data': [ 
     'views/your_.xml file',  
      ],

     'qweb': ['static/src/xml/your_.xml file'],

It will work. Please try it.


Solution 2:

You need to design a Template by your self.

You'll find reference code in odoo it self for create template.

add you template file in manifest file as 'qweb': ["static/src/xml/my_template.xml"]

odoo.define('rhp.main', function (require) {
"use strict";

var core = require('web.core');
var _t = core._t;
var _lt = core._lt;
var Widget = require('web.Widget');
var QWeb = core.qweb;

var Explorer = Widget.extend({
    init: function(parent, options) {
        console.log('Explorer inited');
    },
    start: function() {
        console.log('Explorer started');
    },
});
//exploreraction is must be your template name        
core.action_registry.add('exploreraction', Explorer);

return Explorer;


});

Examplea Template code:

<t t-name="exploreraction" >
    <div id="my_temp_id">
    <div class="ex_button">
        <div class="cancle_btn">
            <button class="btn btn-danger">Cancle</button>
        </div>
        <div class="Add_btn">
            <button class="btn btn-success">Add </button>
        </div>
    </div>
    </div>
</t>

create assets_backend.xml with your other xml files.

/view/assets_backend.xml --> Here you need to add javasscript file path.

 <?xml version="1.0"?>
 <odoo>
   <data>
    <template id="assets_backend" inherit_id="web.assets_backend">
        <xpath expr="script[last()]" position="after">

            <script type="text/javascript" src="/static/src/js/rhp_main.js"></script>
        </xpath>
    </template>
    <template id="assets_common" inherit_id="web.assets_common">
        <xpath expr="script[last()]" position="after">

        </xpath>
    </template>
  </data>
 </odoo>

Solution 3:

I am not familiar with Bounty and I have solution for this link_to_the_screenshot_of_desired_output. It will work fine.

Create your js file in /module_name/static/src/js/your_js_file.js and 
add the following code :


   odoo.define('mypetstore.test_js', function (require) {
    "use strict";

     var Widget = require('web.Widget');
     var core = require('web.core');
     var Model = require('web.Model');

     var ProductsWidget = Widget.extend({
         template: "ProductsWidget",
         init: function(parent, products, color) {
         this._super(parent);
         this.products = products;
         this.color = color;
    },
  });

  var HomePage = Widget.extend({

    start: function() {
        var products = new ProductsWidget(
            this, ["cpu", "mouse", "keyboard", "graphic card", 
                  "screen"], "#00FF00");
        products.appendTo(this.$el);
      },    
    });
    core.action_registry.add('ProductsWidget', HomePage); 
   }); 

Create your xml file in /module_name/static/src/xml/your_xml_file.xml and add the following code:

   <?xml version="1.0"  encoding="UTF-8"?>
     <templates xml:space="preserve">
       <t t-name="ProductsWidget">
         <div>
            <t t-foreach="widget.products" t-as="product">
               <span class="oe_products_item"
                  t-attf-style="background-color: {{ widget.color 
                 }};">
                <t t-esc="product"/>
                </span>
              <br/>
            </t>
         </div>
        </t>
     </templates>

Create your css file in /module_name/static/src/css/your_css_file.css and add the following code:

     .oe_products_item {
     display: inline-block;
     padding: 3px;
     margin: 5px;
     border: 1px solid black;
     border-radius: 3px;
     }

Create your view's xml file in /module_name/views/your_xml_file_.xml and add the following code :

   <?xml version="1.0" encoding="UTF-8"?>
    <odoo>
      <data>
        <template id="assets_backend" name="give_name" 
           inherit_id="web.assets_backend">
          <xpath expr="." position="inside">
           <script type="text/javascript" 
             src="/module_name/static/src/js/your_js_file_name.js">
           </script>
           <link href="/module_name/static/src/css/your_css_file.css" 
             ></link>
           </xpath>
         </template>
         <record id="template_id" model="ir.actions.client">
            <field name="name">ProductsWidget</field>
            <field name="tag">ProductsWidget</field>
        </record>
        <menuitem id="home_page_menu" 
            name="Home Page"
            action="template_id"/>  
       </data>
      </odoo>

Add the following codes in manifest.py:

    'data': [ 
    'views/your_xml_file.xml',
     ],
    'qweb': ['static/src/xml/pet.xml'],

Post a Comment for "Odoo10 Javascript Widget Action Not Working: ClientWidget Is Not A Function"