Odoo 17树形视图按钮添加表头/标签问题求助
Odoo 17 树形视图按钮添加表头标签解决方案
问题背景
Odoo 17 重构了前端列表视图的渲染逻辑,旧版本依赖的_renderHeaderCell方法已被移除,导致此前的教程方案失效。
实现方案
方案一:自定义字段Widget(推荐)
- XML视图配置
在树形视图中为按钮列指定自定义Widget和字段名:
<tree> <field name="action_col" widget="custom_action_widget" /> <!-- 其他业务字段 --> </tree>
- 编写JS组件
创建自定义Widget继承Odoo17的列表字段组件,重写表头内容:
import { FieldList } from '@web/views/fields/field_list/field_list'; import { registry } from '@web/core/registry'; export class CustomActionWidget extends FieldList { get headerCell() { // 返回需要显示的表头文本 return this.env._t('操作'); } } registry.category('fields').add('custom_action_widget', CustomActionWidget);
- 注册组件资源
在模块的__manifest__.py中引入JS文件:
'assets': { 'web.assets_backend': [ 'your_module_name/static/src/js/custom_action_widget.js', ], },
方案二:手动编写表头(快速实现)
直接在树形视图的<thead>中添加对应表头单元格,与按钮列位置对应:
<tree> <thead> <tr> <!-- 其他字段表头 --> <th>操作</th> </tr> </thead> <tbody> <tr> <!-- 其他字段 --> <td> <button type="button" class="btn btn-icon btn-sm" on_click="your_action_method"> <i class="fa fa-check" /> </button> </td> </tr> </tbody> </tree>
注意事项
- 自定义Widget时,需根据字段类型选择正确的父组件(如一对多字段需继承
FieldOne2Many) - 确保JS文件路径在
__manifest__.py中配置正确,否则组件无法加载
内容的提问来源于stack exchange,提问作者HaNgocHieu
相关产品推荐
相关产品推荐

