Odoo 11表单视图按钮触发JS弹窗报错问题求助
Alright, let's work through getting that JavaScript alert to pop up correctly when clicking your button in Odoo 11. Based on the code and setup you shared, here are the key issues you're likely hitting—and how to fix them step by step:
1. First, Fix Your Module's Manifest File
Chances are your JavaScript file isn't being loaded at all because your __manifest__.py is missing the asset configuration. Odoo needs to know where to pull your JS from, so update your manifest to look like this:
{ 'name': 'JS Custom Button Handler', 'version': '1.0', 'author': 'Your Name', 'depends': ['web'], # Required for web module components 'data': [ # Make sure to list your view file here (e.g., 'views/my_view.xml') ], 'assets': { 'web.assets_backend': [ 'js/static/src/js/my_javascript.js', # Exact path to your JS file ], }, 'installable': True, 'auto_install': False, }
2. Update Your JavaScript Code
In Odoo 11, the web.form_widgets module structure changed—directly extending WidgetButton won't work like it did in older versions. Instead, we'll hook into the form controller's button click handler, which is the correct approach for this version. Replace your my_javascript.js code with this:
odoo.define('js.my_javascript', function(require) { "use strict"; var core = require('web.core'); var FormController = require('web.FormController'); FormController.include({ _onButtonClicked: function(ev) { // Grab the button's attributes from the event data var buttonAttrs = ev.data.attrs; // Check if our custom attribute exists if (buttonAttrs.custom === "click") { alert('clicked!'); ev.stopPropagation(); // Stop Odoo from running default button actions (if needed) return; } // Call the original handler for other buttons this._super.apply(this, arguments); }, }); });
3. Correct Your View Button Configuration
Make sure your button in the XML view has the exact custom="click" attribute, and use the right button type depending on your needs:
<?xml version="1.0" encoding="utf-8"?> <odoo> <!-- Example: Inheriting an existing form view to add your button --> <record id="res_partner_form_inherit" model="ir.ui.view"> <field name="name">res.partner.form.inherit.js.button</field> <field name="model">res.partner</field> <!-- Replace with your model --> <field name="inherit_id" ref="base.view_partner_form"/> <!-- Replace with your base view --> <field name="arch" type="xml"> <!-- Add button after the last button in the form --> <xpath expr="//button[last()]" position="after"> <!-- Use type="button" for pure frontend actions (no backend call needed) --> <button string="Show Alert" type="button" custom="click"/> <!-- OR use type="object" if you need a backend method (add the method to your model) --> <!-- <button string="Show Alert" type="object" name="dummy_method" custom="click"/> --> </xpath> </field> </record> </odoo>
4. Optional: Add a Dummy Backend Method (If Using type="object")
If you went with type="object" in your button, you need an empty method in your model to avoid Odoo throwing an error. Add this to your model's Python file:
from odoo import models class ResPartner(models.Model): _inherit = 'res.partner' # Replace with your model def dummy_method(self): # No logic needed here—just a placeholder return True
Final Troubleshooting Steps
- Clear Caches: Always clear Odoo's cache (Settings > Technical > User Interface > Clear Cache) and your browser's cache after making changes to static files or views.
- Check Browser Console: Open your browser's dev tools (F12) and look for errors—this will tell you if your JS module isn't loading or if there's a syntax issue.
- Verify Module Installation: Make sure your
jsmodule is installed in Odoo.
That should get your alert popping up when you click the button!
内容的提问来源于stack exchange,提问作者fawad

