如何在Odoo社区版发票列表视图实现阶段数量统计
实现Odoo发票模块列表视图阶段数量统计(仿采购模块)
1. 确认模型阶段字段
确保你的发票模型已定义明确的阶段选择字段,示例代码:
from odoo import models, fields class InvoiceCustom(models.Model): _name = 'invoice.custom' _description = '自定义发票' state = fields.Selection([ ('draft', '草稿'), ('awaiting_approval', '待审批'), ('done', '已完成') ], string='状态', default='draft')
2. 自定义JS控制器(核心实现)
在模块的static/src/js目录下创建invoice_dashboard_list.js,复制采购模块的统计逻辑并适配你的模型:
odoo.define('your_module.invoice_dashboard_list', function (require) { "use strict"; const ListController = require('web.ListController'); const viewRegistry = require('web.view_registry'); const core = require('web.core'); const _t = core._t; const InvoiceDashboardList = ListController.extend({ renderButtons: function () { this._super.apply(this, arguments); if (this.$buttons) { this._renderDashboardButtons(); } }, _renderDashboardButtons: function () { const self = this; const stateField = 'state'; const states = { draft: {label: _t('草稿'), count: 0}, awaiting_approval: {label: _t('待审批'), count: 0}, done: {label: _t('已完成'), count: 0} }; // 统计当前列表各阶段数量 this.model.get(this.handle).then(function (result) { const records = result.data.records; records.forEach(record => { const state = record.data[stateField]; if (states[state]) states[state].count++; }); // 构建统计按钮HTML let dashboardHtml = '<div class="o_purchase_dashboard_buttons btn-group">'; Object.keys(states).forEach(state => { const data = states[state]; dashboardHtml += ` <button type="button" class="btn btn-secondary o_dashboard_button" data-state="${state}"> ${data.label} <span class="badge badge-pill badge-primary">${data.count}</span> </button> `; }); dashboardHtml += '</div>'; // 插入到列表顶部 self.$buttons.before(dashboardHtml); // 绑定过滤事件 self.$('.o_dashboard_button').on('click', function () { const targetState = $(this).data('state'); self.do_action({ type: 'ir.actions.act_window', res_model: self.modelName, view_mode: 'list', domain: [[stateField, '=', targetState]], context: self.context, }); }); }); }, }); viewRegistry.add('invoice_dashboard_list', InvoiceDashboardList); return InvoiceDashboardList; });
3. 注册JS资源
在模块的__manifest__.py中添加JS文件引用:
'assets': { 'web.assets_backend': [ 'your_module/static/src/js/invoice_dashboard_list.js', ], },
4. 绑定列表视图JS类
修改你的发票列表视图XML,指定自定义的JS控制器:
<record id="invoice_custom_list_view" model="ir.ui.view"> <field name="name">invoice.custom.list</field> <field name="model">invoice.custom</field> <field name="arch" type="xml"> <tree js_class="invoice_dashboard_list"> <field name="name"/> <field name="state"/> <!-- 其他列表字段 --> </tree> </field> </record>
5. 样式适配(可选)
如果需要和采购模块样式完全一致,添加CSS文件static/src/css/invoice_dashboard.css:
.o_purchase_dashboard_buttons .o_dashboard_button { margin-right: 8px; } .o_purchase_dashboard_buttons .badge { margin-left: 4px; }
并在__manifest__.py的assets中补充引用:
'web.assets_backend': [ 'your_module/static/src/js/invoice_dashboard_list.js', 'your_module/static/src/css/invoice_dashboard.css', ],
关键说明
- 核心逻辑是继承Odoo的
ListController,重写按钮渲染方法统计当前列表的各阶段记录数,生成带数量的筛选按钮。 - 替换代码中的
your_module为你的模块名称,invoice.custom为发票模型名称,确保阶段字段名与模型定义一致。 - 点击统计按钮会自动过滤对应阶段的发票,和采购模块的交互逻辑完全一致。
内容的提问来源于stack exchange,提问作者Boris Veselov
相关产品推荐
相关产品推荐

