目录
1、什么是tree视图
Tree视图是一种用于显示记录列表的视图类型。它通常用于显示数据库中的多个记录,并以表格的形式呈现数据。Tree视图是Odoo中最常见和灵活的视图之一。
2、源码Tree视图详解
1、源码
本次以CRM中的渠道Tree视图做为讲解
源码为:
<record id="crm_case_tree_view_oppor" model="ir.ui.view">
<field name="name">crm.lead.tree.opportunity</field>
<field name="model">crm.lead</field>
<field name="priority">1</field>
<field name="arch" type="xml">
<tree string="Opportunities" sample="1" multi_edit="1">
<header>
<button name="%(crm.action_lead_mass_mail)d" type="action" string="Email" />
</header>
<field name="company_id" invisible="1"/>
<field name="user_company_ids" invisible="1"/>
<field name="date_deadline" invisible="1"/>
<field name="create_date" optional="hide"/>
<field name="name" string="Opportunity" readonly="1"/>
<field name="partner_id" optional="hide"/>
<field name="contact_name" optional="show"/>
<field name="email_from"/>
<field name="phone" class="o_force_ltr"/>
<field name="company_id" groups="base.group_multi_company" optional="show"/>
<field name="city" optional="hide"/>
<field name="state_id" optional="hide"/>
<field name="country_id" optional="hide" options="{'no_open': True, 'no_create': True}"/>
<!-- Explicit domain due to multi edit -> real company domain would be complicated -->
<field name="user_id" widget="many2one_avatar_user" optional="show"
domain="[('share', '=', False)]"/>
<field name="team_id" optional="hide"/>
<field name="priority" optional="hide" widget="priority"/>
<field name="activity_ids" widget="list_activity"/>
<field name="activity_user_id" optional="hide" string="Activity by" widget="many2one_avatar_user"/>
<field name="my_activity_date_deadline" string="My Deadline" widget="remaining_days" options="{'allow_order': '1'}"/>
<field name="activity_calendar_event_id" invisible="1"/>
<field name="campaign_id" optional="hide"/>
<field name="medium_id" optional="hide"/>
<field name="source_id" optional="hide"/>
<field name="company_currency" invisible="1"/>
<field name="expected_revenue" sum="Expected Revenues" optional="show" widget="monetary" options="{'currency_field': 'company_currency'}"/>
<field name="date_deadline" optional="hide"/>
<field name="recurring_revenue_monthly" sum="Expected MRR" optional="show" widget="monetary"
options="{'currency_field': 'company_currency'}" groups="crm.group_use_recurring_revenues"/>
<field name="recurring_revenue" sum="Recurring Revenue" optional="hide" widget="monetary"
options="{'currency_field': 'company_currency'}" groups="crm.group_use_recurring_revenues"/>
<field name="recurring_plan" optional="hide" groups="crm.group_use_recurring_revenues"/>
<field name="stage_id" optional="show" decoration-bf="1"/>
<field name="active" invisible="1"/>
<field name="probability" string="Probability (%)" optional="hide"/>
<field name="tag_ids" optional="hide" widget="many2many_tags" options="{'color_field': 'color'}"/>
<field name="referred" invisible="1"/>
<field name="message_needaction" invisible="1"/>
<button name="%(crm.action_lead_mail_compose)d" type="action" string="Email" icon="fa-envelope"/>
<button name="action_reschedule_meeting" class="text-warning" type="object" string="Reschedule"
icon="fa-calendar" attrs="{'invisible': ['|', ('my_activity_date_deadline', '=', False),
('activity_calendar_event_id', '=', False)]}"/>
<button name="action_snooze" class="text-warning" type="object" string="Snooze 7d"
icon="fa-bell-slash" attrs="{'invisible': ['|', ('my_activity_date_deadline', '=', False),
('activity_calendar_event_id', '!=', False)]}"/>
</tree>
</field>
</record>
2、参数讲解
-
<record id="crm_case_tree_view_oppor" model="ir.ui.view">
: 定义一个ir.ui.view记录,其唯一标识为"crm_case_tree_view_oppor",属于"ir.ui.view"模型。 -
<field name="name">crm.lead.tree.opportunity</field>
: 设置视图的名称为"crm.lead.tree.opportunity"。 -
<field name="model">crm.lead</field>
: 设置该视图关联的模型为"crm.lead",即CRM模块中的机会模型。 -
<field name="priority">1</field>
: 设置优先级为1,表示这个视图的优先级。 -
<field name="arch" type="xml">
: 定义了视图的结构部分,类型为XML。 -
<tree string="Opportunities" sample="1" multi_edit="1">
: 开始定义Tree视图,设置标题为"Opportunities",sample
和multi_edit
分别表示启用示例模式和多编辑模式。 -
<header>
: 定义Tree视图的头部,用于显示一些操作按钮等。 -
<button name="%(crm.action_lead_mass_mail)d" type="action" string="Email" />
: 在头部添加一个按钮,点击后执行crm.action_lead_mass_mail动作,显示"Email"。 -
<field name="company_id" invisible="1"/>
: 设置字段company_id在Tree视图中不可见。 -
<field name="user_company_ids" invisible="1"/>
: 设置字段user_company_ids在Tree视图中不可见。 -
<field name="date_deadline" invisible="1"/>
: 设置字段date_deadline在Tree视图中不可见。 -
...(后续为一系列字段的定义,包括name、partner_id等等)
-
<field name="stage_id" optional="show" decoration-bf="1"/>
: 定义字段stage_id,并且在Tree视图中可选显示(optional="show"),decoration-bf
表示在字段周围显示背景标志。 -
<field name="active" invisible="1"/>
: 设置字段active在Tree视图中不可见。 -
<field name="probability" string="Probability (%)" optional="hide"/>
: 定义字段probability,在Tree视图中可选隐藏,设置显示名称为"Probability (%)"。 -
<field name="tag_ids" optional="hide" widget="many2many_tags" options="{'color_field': 'color'}"/>
: 定义字段tag_ids,可选隐藏,使用many2many_tags小部件,根据color字段设置标签的颜色。
3、Tree视图中<field>标签的常用参数详解
-
name: 字段的名称,指定在模型中的字段。
-
string: 用于显示在Tree视图表头的字段标签的文本。
-
invisible: 控制字段是否可见。可选值为"1"(不可见)或"0"(可见)。
-
optional: 控制字段是否可选。可选值为"hide"(不可选)或"show"(可选)。
-
widget: 指定在Tree视图中使用的小部件,影响字段的显示方式。例如,"many2one_avatar_user"。
-
options: 提供一些小部件相关的选项,以字典形式表示。
-
class: 添加额外的CSS类,用于自定义样式。
-
sum: 在Tree视图底部显示字段的总和,通常用于数字字段。
-
groups: 通过用户组控制字段的可见性,只有属于指定用户组的用户才能看到该字段。
-
readonly: 控制字段是否为只读。可选值为"1"(只读)或"0"(可编辑)。
-
decoration-bf: 在字段周围显示背景标志。可选值为"1"(显示)或"0"(不显示)
4、<tree>标签参数详解
1、string:设置Tree视图的标题,该标题将显示在页面顶部
<tree string="My Tree View">
2、editable:指定Tree视图是否可编辑。
<tree editable="bottom">
create:指定是否显示“创建”按钮以创建新记录
<tree create="true">
delete:指定是否显示“删除”按钮以删除选定的记录
<tree delete="true">
default_order:设置Tree视图的默认排序规则
<tree default_order="date desc, name">
decoration-muted:定义一个域列表,表示记录行是否显示为灰色
<tree decoration-muted="{'in_progress': [('state','=','draft')]}" string="My Tree View">
decoration-danger:定义一个域列表,表示记录行是否显示为红色
<tree decoration-danger="{'overdue': [('date_deadline','<',time.strftime('%Y-%m-%d'))]}" string="My Tree View">
decoration-warning:定义一个域列表,表示记录行是否显示为黄色
<tree decoration-warning="{'warning': [('date_deadline','=',time.strftime('%Y-%m-%d'))]}" string="My Tree View">
3、Tree视图中的按钮
1、表头按钮
通过在<tree>
标签下的<header>
标签内部定义按钮,将按钮显示在Tree视图的表头部分
<tree>
<header>
<button name="your_button_function" string="Your Button" type="object"/>
</header>
<!-- Fields and other configurations go here -->
</tree>
2、记录行按钮
通过在<tree>
标签下的<button>
标签内部定义按钮,将按钮显示在每条记录行的最右侧
<tree>
<!-- Fields and other configurations go here -->
<button string="Your Action Button" type="object" name="your_button_function" class="oe_highlight"/>
</tree>