如何在Kanban视图和Tree视图顶部添加按钮并实现无选全量打印报表
Hey there! Let's tackle how to add that print button to your Kanban and Tree views (I’m assuming you’re working with Odoo here, given the view types you mentioned) that prints all records without needing to select any first. Here’s a practical, step-by-step breakdown:
First, you need a report that can generate the printout for all records. Let’s use a QWeb PDF report as an example—this is the standard approach in Odoo.
Define the Report Action
Add this XML to your module’s report definition file:
<report id="report_all_records" model="your.model.name" <!-- Replace with your actual model (e.g., sale.order) --> string="Print All Records" report_type="qweb-pdf" name="your_module.report_all_records_template" file="your_module.report_all_records_template" attachment_use="False" />
Create the QWeb Template
Next, make the template that renders the report content. Save this in a separate XML file (or the same one as above):
<template id="report_all_records_template"> <t t-call="web.html_container"> <t t-foreach="docs" t-as="doc"> <div class="page"> <!-- Customize this with your model's fields --> <h3><t t-esc="doc.name"/></h3> <p>Created on: <t t-esc="doc.create_date"/></p> <hr/> </div> </t> </t> </template>
Now we’ll inject the button into the header section of both Tree and Kanban views using XML inheritance.
For Tree View
<record id="your_model_tree_inherit" model="ir.ui.view"> <field name="name">your.model.tree.view.inherit</field> <field name="model">your.model.name</field> <field name="inherit_id" ref="your_module.base_tree_view_id"/> <!-- Replace with your base Tree view ID --> <field name="arch" type="xml"> <xpath expr="//tree" position="inside"> <header> <button name="%(your_module.report_all_records)d" string="Print All Records" type="action" class="oe_highlight" context="{'active_ids': env['your.model.name'].search([]).ids}"/> </header> </xpath> </field> </record>
For Kanban View
<record id="your_model_kanban_inherit" model="ir.ui.view"> <field name="name">your.model.kanban.view.inherit</field> <field name="model">your.model.name</field> <field name="inherit_id" ref="your_module.base_kanban_view_id"/> <!-- Replace with your base Kanban view ID --> <field name="arch" type="xml"> <xpath expr="//kanban" position="inside"> <header> <button name="%(your_module.report_all_records)d" string="Print All Records" type="action" class="oe_highlight" context="{'active_ids': env['your.model.name'].search([]).ids}"/> </header> </xpath> </field> </record>
- No Selection Needed: The
contextusesenv['your.model.name'].search([]).idsto fetch every record in the model and pass them directly to the report—no manual selection required. - Styling: The
oe_highlightclass makes the button stand out, but you can remove it or use a custom class if you want a different look. - Permissions: Double-check that your user has access rights to both the model and the report action (you can set this in your module’s security files).
If you need to filter records or add extra processing before printing, create a Python method in your model instead:
Python Code
from odoo import models, api class YourModel(models.Model): _inherit = 'your.model.name' def action_print_all_records(self): # Add custom filtering here if needed (e.g., only active records) records = self.search([('active', '=', True)]) # Trigger the report with the filtered records return self.env.ref('your_module.report_all_records').report_action(records)
Updated Button XML
Replace the button code in your view inheritance with this:
<button name="action_print_all_records" string="Print All Records" type="object" class="oe_highlight"/>
This gives you full control over which records get included in the printout.
内容的提问来源于stack exchange,提问作者Prashanth K

