我想在窗体视图中按钮单击时显示JavaScript警报,但在odoo 11中显示错误

时间:2018-04-12 08:10:56

标签: odoo

模块名称:js

Javascript文件路径js / static / src / js / my_javascript.js

odoo.define('js.my_javascript', function(require) { "use strict";

var form_widget = require('web.form_widgets'); var core = require('web.core'); var _t = core._t; var QWeb = core.qweb;

form_widget.WidgetButton.include({
    on_click: function() {
         if(this.node.attrs.custom === "click"){

            alert('clicked');

            return;
         }
         this._super();
    }, }); });

Views文件夹有2个文件

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


<odoo>
    <data>

        <record model="ir.ui.view" id="js_form_view">
            <field name="name">js.form</field>
            <field name="model">js</field>
            <field name="arch" type="xml">
                <form string="js">
                    <header>
                       <button string="Click" custom="click"/>
                    </header>
                         <sheet>
                             <h1><field name="name" string="New" nolabel="1"></field></h1>
                        </sheet>
                </form>
            </field>
        </record>
      <act_window id="js_action" name="js" res_model="js" />
        <menuitem id="main_js_menu" name="js" action="js_action" />

    </data> </odoo>

错误: 附加图片enter image description here

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

表单视图:

<odoo>
<data>

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

    <record model="ir.ui.view" id="view_my_model_form">
        <field name="name">my.model.name.form</field>
        <field name="model">my.model.name</field>
        <field name="arch" type="xml">
            <form string="Expense Type">
                <sheet>
                    <group>
                        <widget name="my_widget"/>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

</data>
</odoo>

JavaScript

odoo.define('my_module.my_widget', function (require){ 
    var Widget= require('web.Widget'); 
    var widgetRegistry = require('web.widget_registry'); 
    var FieldManagerMixin = require('web.FieldManagerMixin'); 
    var MyWidget = Widget.extend(FieldManagerMixin, { 
        init: function (parent, model, context) 
        { 
            this._super(parent); 
            FieldManagerMixin.init.call(this); 
            this._super.apply(this, arguments); 
        }, 
        start: function() { 
            var self = this; 
            this._super.apply(this, arguments); 
            var html ='<button id="btn_submit" class="btn btn-primary">Add Previous Compliance</button>'; 
            this.$el.html(html); 
            this.$('#btn_submit').click(function(ev){ 
                ev.preventDefault(); 
                ev.stopPropagation(); 
                alert("Submit button clicked"); 
            }); 
        }, 
    }); 
    widgetRegistry.add( 'my_widget', MyWidget ); 
});