Odoo报表树视图头部添加按钮失败:页面空白无报错求助
问题分析与修复
问题现象
在报表头部面板添加新按钮时,模块更新无报错,但打开报表页面后完全卡死,页面空白且无任何报错信息。
错误原因
- 模板继承目标错误:原模板继承的是
web.ListView,但现代Odoo版本中,列表视图的按钮模板应继承web.ListView.Buttons,错误的继承目标会引发模板渲染冲突,导致页面卡死。 - XPath定位与位置设置不当:原XPath使用
position="after"将按钮添加到control-panel-additional-actionsslot之后,破坏了原有控制栏的DOM结构,引发渲染异常。正确操作是将按钮添加到该slot内部。
修正后的代码
1. XML模板修正
<?xml version="1.0" encoding="utf-8"?> <templates> <t t-name="stock_button_report.ListView.Buttons" t-inherit="web.ListView.Buttons"> <xpath expr="t[@t-set-slot='control-panel-additional-actions']" position="inside"> <button type="button" class="btn btn-primary" style="margin-left: 10px;" t-on-click="OnTestClick"> Example </button> </xpath> </t> </templates>
2. JS代码(验证正确性,可补充点击逻辑)
/** @odoo-module */ import { ListController } from "@web/views/list/list_controller"; import { registry } from "@web/core/registry"; import { listView } from "@web/views/list/list_view"; export class StockButtonListController extends ListController { setup() { super.setup(); } OnTestClick() { // 可在此添加按钮点击逻辑 console.log("按钮被点击"); } } registry.category("views").add("stock_button_report", { ...listView, Controller: StockButtonListController, buttonTemplate: "stock_button_report.ListView.Buttons", });
3. 视图部分(修正继承视图命名,优化可读性)
<record id="view_report_history_tree" model="ir.ui.view"> <field name="name">report.history.tree</field> <field name="model">report.history</field> <field name="arch" type="xml"> <tree string="Historial de Informes"> <field name="name" /> <field name="create_date" /> <field name="user_id" /> <field name="file_name" /> <button name="action_download_report" type="object" icon="fa-download" string="Descargar" /> </tree> </field> </record> <record id="view_report_history_tree_inherit" model="ir.ui.view"> <field name="name">report.history.tree.inherit</field> <field name="model">report.history</field> <field name="inherit_id" ref="view_report_history_tree" /> <field name="arch" type="xml"> <xpath expr="//tree" position="attributes"> <attribute name="js_class">stock_button_report</attribute> </xpath> </field> </record>
补充说明
- 确保模块manifest文件中正确声明了
web等必要依赖。 - 清理浏览器缓存并重启Odoo服务后,重新更新模块即可生效。
内容的提问来源于stack exchange,提问作者MIGUEL ANGEL GONZALEZ PINTO
相关产品推荐
相关产品推荐

