0

I want to make a handler for button in header using java script. My view model is given below :

     <template id="assets_backend" name="petstore" 
        inherit_id="web.assets_backend">
        <xpath expr="." position="inside">

     <script type="text/javascript" 
        src="/mypetstore/static/src/js/model_access.js">
     </script>

     <link href="/mypetstore/static/src/css/petstore.css" 
         rel="stylesheet">
     </link>
        </xpath>
    </template> 



    <record model="ir.ui.view" id="my_pet_store_form">
        <field name="name">my_pet_store_form</field>
        <field name="model">petstore.message</field>
        <field name="type">form</field>
        <field name="arch" type="xml">
        <header>
         <button name="click_me" string="Click" 
              class="oe_highlight"/>
         </header>
            <form string="Message of the day">

                <group col="2">
                    <group>
                        <field name="data"/>
                    </group>
                </group>
            </form>
          </field>
       </record>

When user click on 'click_me' button, then it call a simple javascript function or action client. Just print an alert. JS part : odoo.define('mypetstore.model_access', function (require) { "use strict"; var Class = require('web.Class'); var Widget = require('web.Widget'); var core = require('web.core'); var utils = require('web.utils');

    jq('#click_me').bind('click', function(){
       alert("hello");
      });  
    });
1
  • It's "JavaScript", not "java script". Commented Jul 18, 2017 at 13:27

2 Answers 2

1

First I would suggest you to give some ID attribute to Object, you want to use. like

 <button name="click_me" id="click_me" string="Click" 
              class="oe_highlight"/>

JQuery:

jq('#click_me').bind('click', function(){
   alert("hello");
});
Sign up to request clarification or add additional context in comments.

5 Comments

Getting error : "error: Some modules could not be started "
I added this code in my js file and getting the error. I have written my js code with the above code
use $ instead, I have rename JQuery object to jq. Sorry for inconvenience, instead, jq('#click_me') use $('#click_me')
but.. its not working.. do we need to add those buttons inside template tag ?
getting this errorjQuery.fn.init {context: document, selector: "#click_me"}
0

This is how your button should looks like, just add onclick whish is your handler.

         <header>
           <button onclick="myFunction()" name="click_me" string="Click" 
              class="oe_highlight"/>
         </header>

myFunction() will be your javascript code.

Now the alert code will be this:

<script type = "text/ javascript">  
  function myFunction() {
    alert("Hello! I am an alert box!!");
  }
</script>  

hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.