Odoo 10 XML树形视图中筛选Employee模型记录的方法
Hey there! To get your Employee tree view to only display staff from the HR, Audit, and Production departments, there are a couple of simple, effective ways to set this up in your XML configuration. Let me walk you through them:
Option 1: Add a Filter Directly to the Tree View
You can embed a domain attribute right in the <tree> tag of your view. This applies the filter every time the view is loaded.
Here's a complete example of the view definition:
<record id="view_employee_filtered_tree" model="ir.ui.view"> <field name="name">employee.filtered.tree</field> <field name="model">employee</field> <field name="arch" type="xml"> <!-- The domain here restricts records to the specified departments --> <tree string="Filtered Employees" domain="[('e_dept', 'in', ['HR', 'Audit', 'Production'])]"> <field name="e_name"/> <field name="e_num"/> <field name="e_addr"/> <field name="e_dept"/> </tree> </field> </record>
What the Domain Does:
The domain expression [('e_dept', 'in', ['HR', 'Audit', 'Production'])] tells the system to only fetch records where the e_dept field matches one of the values in the list.
Option 2: Set the Filter in the Action (Menu Entry)
If you want the filter to apply whenever users access the employee list via a specific menu item, you can define the domain in the ir.actions.act_window record instead. This way, the filter is tied to the action, not just the view.
Example action configuration:
<record id="action_employee_filtered_list" model="ir.actions.act_window"> <field name="name">Filtered Employees</field> <field name="res_model">employee</field> <field name="view_mode">tree</field> <!-- Apply the domain here --> <field name="domain">[('e_dept', 'in', ['HR', 'Audit', 'Production'])]</field> <!-- Link to your tree view --> <field name="view_id" ref="view_employee_filtered_tree"/> </record> <!-- Optional: Add the action to a menu --> <menuitem name="Filtered Employees" id="menu_employee_filtered" action="action_employee_filtered_list"/>
Important Note for Related Department Fields
If your e_dept is a relational field (e.g., linked to a hr.department model instead of a plain string), you'll need to adjust the domain to target the name field of the related model:
domain="[('e_dept.name', 'in', ['HR', 'Audit', 'Production'])]"
Hope this gets you sorted! If you run into any hiccups with field types or view inheritance, feel free to follow up.
内容的提问来源于stack exchange,提问作者Mingg Lex




