8、基本视图(11)
基本视图
视图定义显示模型记录的方式。每种类型的视图都代表一种可视化的模式(记录的列表,其聚合的图形,…)。视图可以通过它们的类型(例如一个合作伙伴列表)或通过其id进行通用请求。对于一般请求,使用正确类型和最低优先级的视图将被使用(因此每种类型的最低优先级视图是该类型的默认视图)
视图继承允许更改在其他地方声明的视图(添加或删除内容)
通用视图声明
视图的声明是作为模块 ir.ui.view 的一条记录,也是在数据库中ir.ui.view 中插入记录来实现的。视图类型是由arch字段的根元素隐含的。
<record model="ir.ui.view" id="view_id"> <field name="name">view.name</field> <field name="model">object_name</field> <field name="priority" eval="16"/> <field name="arch" type="xml"> <!-- view content: <form>, <tree>, <graph>, ... --> </field> </record>
危险!
这个视图的内容是 XML.
arch 字段 因此必须被声明为type="xml" 才能被解析正确
注:
<record model="ir.ui.view" id="view_id"> id是唯一的值,每条记录都不一样,不要重复
view.name 是视图的名字可以自己命名
<field name="priority" eval="16"/> 优先级数值设置的越小,显示的优先级越高,比如设置为0则默认显示该视图,也可以手工指定显示的默认视图,如果只定义了一个视图,那么此条代码可以删除。
form表单视图 tree列表视图 graph图表视图
树视图
树视图,也称为列表视图,以表格形式显示记录。
它们的根元素是<tree>树视图最简单的形式就是列出表中显示的所有字段(每个字段为列):
<tree string="Idea list"> <field name="name"/> <field name="inventor_id"/> </tree>
以下代码加载后以树形视图把name和description字段全部显示出来:
<odoo> <data> <record model="ir.ui.view" id="mycourse_view"> <field name="name">mycourse_view_name</field> <field name="model">mymodule.mymodule</field> <field name="priority" eval="16"/> <field name="arch" type="xml"> <!-- view content: <form>, <tree>, <graph>, ... --> <tree string="mycourse_tree"> <field name="name"/> <field name="description"/> </tree> </field> </record> </data> </odoo>
表单视图
表单用于创建和编辑单个记录
他们的根元素是<form>,它们由高级结构元素(groups, notebooks)和交互元素(buttons and fields)组成
<form string="Idea form"> <group colspan="4"> <group colspan="2" col="2"> <separator string="General stuff" colspan="2"/> <field name="name"/> <field name="inventor_id"/> </group> <group colspan="2" col="2"> <separator string="Dates" colspan="2"/> <field name="active"/> <field name="invent_date" readonly="1"/> </group> <notebook colspan="4"> <page string="Description"> <field name="description" nolabel="1"/> </page> </notebook> <field name="state"/> </group> </form>
练习:
为课程对象创建自己的表单视图。所显示的数据按顺序是:该课程的名称和描述。
<odoo> <data> <record model="ir.ui.view" id="mycourse_view"> <field name="name">mycourse_view_name</field> <field name="model">mymodule.mymodule</field> <field name="priority" eval="16"/> <field name="arch" type="xml"> <!-- view content: <form>, <tree>, <graph>, ... --> <tree string="mycourse_tree"> <field name="name"/> <field name="description"/> </tree> </field> </record> <record model="ir.ui.view" id="mycourse_form_view"> <field name="name">mycourse.form</field> <field name="model">mymodule.mymodule</field> <field name="arch" type="xml"> <form string="Course Form"> <sheet> <group> <field name="name"/> <field name="description"/> </group> </sheet> </form> </field> </record> </data> </odoo>
<sheet>和<group>标签显示效果:

在课程表单视图中,将description字段放在一个选项卡下,这样就更容易添加其他选项卡,包含额外的信息。
<?xml version="1.0" encoding="UTF-8"?> <odoo> <data> <record model="ir.ui.view" id="mycourse_view"> <field name="name">mycourse_view_name</field> <field name="model">mymodule.mymodule</field> <field name="priority" eval="16"/> <field name="arch" type="xml"> <!-- view content: <form>, <tree>, <graph>, ... --> <tree string="mycourse_tree"> <field name="name"/> <field name="description"/> </tree> </field> </record> <record model="ir.ui.view" id="mycourse_form_view"> <field name="name">mycourse.form</field> <field name="model">mymodule.mymodule</field> <field name="arch" type="xml"> <form string="Course Form"> <sheet> <group> <field name="name"/> </group> <notebook> <page string="描述"> <field name="description"/> </page> <page string="关于"> This is an example of notebooks </page> </notebook> </sheet> </form> </field> </record> </data> </odoo>
效果如下图所示:

表单视图还可以使用纯HTML来实现更灵活的布局。
<form string="Idea Form"> <header> <button string="Confirm" type="object" name="action_confirm" states="draft" class="oe_highlight" /> <button string="Mark as done" type="object" name="action_done" states="confirmed" class="oe_highlight"/> <button string="Reset to draft" type="object" name="action_draft" states="confirmed,done" /> <field name="state" widget="statusbar"/> </header> <sheet> <div class="oe_title"> <label for="name" class="oe_edit_only" string="Idea Name" /> <h1><field name="name" /></h1> </div> <separator string="General" colspan="2" /> <group colspan="2" col="2"> <field name="description" placeholder="Idea description..." /> </group> </sheet> </form>
搜索视图
搜索视图自定义与列表视图相关的搜索字段(和其他聚合视图)。它们的根元素是<search>,定义哪些字段可以用来搜索:
例如:
<search> <field name="name"/> <field name="inventor_id"/> </search>
对于模型,没有搜索视图,Odoo生成一个只允许在name字段中搜索的视图
<record model="ir.ui.view" id="mycourse_search_view"> <field name="name">mycourse.search</field> <field name="model">mymodule.mymodule</field> <field name="arch" type="xml"> <search> <field name="name"/> <field name="description"/> </search> </field> </record>
效果如下图:

浙公网安备 33010602011771号