Design Elements

文件中的视图描述形式如下:

Example:
  1. <?xml version="1.0"?>
  2. <openerp>
  3. <data>
  4. [view definitions]
  5. </data>
  6. </openerp>

视图定义包含了以下三种类型的标签:

  • 标签有属性model=“ir.ui.view”,它包含本身的视图定义

  • 标签有属性model=“ir.actions.act_window”,它将动作和这些视图链接起来。

  • 标签,在菜单中创建记录,将他们和动作链接起来。

New:你可以指定组,菜单在menuitem标签中用组属性来访问这些组。

New:你可以使用shortcut标签来加快捷键。

Example:
  1. <shortcut
  2. name="Draft Purchase Order (Proposals)"
  3. model="purchase.order"
  4. logins="demo"
  5. menu="m"/>

你应该加一个id属性在menuitem上

  1. <record model="ir.ui.view" id="v">
  2. <field name="name">sale.order.form</field>
  3. <field name="model">sale.order</field>
  4. <field name="priority" eval="2"/>
  5. <field name="arch" type="xml">
  6. <form string="Sale Order">
  7. .........
  8. </form>
  9. </field>
  10. </record>

priority字段的默认值为16,在没有特别规定的前提下,系统的视图都是最低优先级。

View Types

Tree View

你可以指定列表中包含的列元素和一些细节在列的显示中。search字段不能在这指定,他们显示在form视图中。

  1. <record id="view_location_tree2" model="ir.ui.view">
  2. <field name="name">stock.location.tree</field>
  3. <field name="model">stock.location</field>
  4. <field name="type">tree</field>
  5. <field name="priority" eval="2"/>
  6. <field name="arch" type="xml">
  7. <tree
  8. colors="blue:usage=='view';darkred:usage=='internal'">
  9. <field name="complete_name"/>
  10. <field name="usage"/>
  11. <field
  12. name="stock_real"
  13. invisible="'product_id' not in context"/>
  14. <field
  15. name="stock_virtual"
  16. invisible="'product_id' not in context"/>
  17. </tree>
  18. </field>
  19. </record>

这个例子是一个二维列表,但是你可以通过指点field_parent来显示树结构。这个名字尽管有些误导人,但是该字段必须包含一列所有的child记录。

  1. <record id="view_location_tree" model="ir.ui.view">
  2. <field name="name">stock.location.tree</field>
  3. <field name="model">stock.location</field>
  4. <field name="type">tree</field>
  5. <field name="field_parent">child_ids</field>
  6. <field name="arch" type="xml">
  7. <tree toolbar="1">
  8. <field icon="icon" name="name"/>
  9. </tree>
  10. </field>
  11. </record>

在tree元素中包含下面的属性:

colors

Conditions for applying different colors to items in the list. The default is black.

toolbar

如果你想要一个树结构在单独工具栏区域列出上层的记录,那就把给属性值设为1。当你点击工具栏上的一个记录时,它的所有的后代都会显示在main tree中。二维列表中该值可以忽略

Grouping Elements

Separator

加个分隔线

Example:
  1. <separator string="Links" colspan="4"/>

string属性定义了它的标签,colspan属性定义了他的水平大小(列的个数)。

Notebook

: 用notebooks你可以分配视图字段在不同的标签上(每个由page tag定义)。你可以使用tabpos属性来设置tab:up,down,left,right。

Example:
  1. <notebook colspan="4">....</notebook>

Group

: 对几个列进行分组。

  • colspan: the number of columns to use

  • rowspan: the number of rows to use

  • expand: if we should expand the group or not

  • col: the number of columns to provide (to its children)

  • string: (optional) If set, a frame will be drawn around the group of fields, with a label containing the string. Otherwise, the frame will be invisible.

Example:
  1. <group col="3" colspan="2">
  2. <field name="invoiced" select="2"/>
  3. <button colspan="1" name="make_invoice" states="confirmed" string="Make Invoice"
  4. type="object"/>
  5. </group>

Page

为视图定义新的notebook

Example:
  1. <page string="Order Line"> ... </page>:
  • string: 定义page的名称

Data Elements

Field

attributes for the “field” tag

  • select=”1”: mark this field as being one of the search criteria for

    this resource’s search view. A value of 1 means that the field is included in the basic search, and a value of 2 means that it is in the advanced search.

  • colspan=”4”: the number of columns on which a field must extend.

  • readonly=”1”: set the widget as read only

  • required=”1”: the field is marked as required. If a field is marked as required, a user has to fill it the system won’t save the resource if the field is not filled. This attribute supersede the required field value defined in the object.

  • nolabel=”1”: hides the label of the field (but the field is not hidden in the search view).

  • invisible=”True”: hides both the label and the field.

  • password=”True”: replace field values by asterisks, “*“.

  • string=””: change the field label. Note that this label is also used in the search view: see select attribute above).

  • domain: can restrict the domain.

    • Example: domain=”[(‘partner_id’,’=’,partner_id)]“
  • widget: can change the widget.

    • Example: widget=”one2many_list”

      • one2one_list

      • one2many_list

      • many2one_list

      • many2many

      • url

      • email

      • image

      • float_time

      • reference

  • mode: sequences of the views when switching.

    • Example: mode=”tree,graph”
  • on_change: define a function that is called when the content of the field changes.

    • Example: on_change=”onchange_partner(type,partner_id)”

    • See ViewsSpecialProperties for details

  • attrs: Permits to define attributes of a field depends on other fields of the same window. (It can be use on page, group, button and notebook tag also)

    • Format: “{‘attribute’:[(‘field_name’,’operator’,’value’),(‘field_name’,’operator’,’value’)],’attribute2’:[(‘field_name’,’operator’,’value’),]}”

    • where attribute will be readonly, invisible, required

    • Default value: {}.

    • Example: (in product.product)

    1. <field digits="(14, 3)" name="volume" attrs="{'readonly':[('type','=','service')]}"/>
  • eval: evaluate the attribute content as if it was Python code (see below for example)

  • default_focus: set to 1 to put the focus (cursor position) on this field when the form is first opened. There can only be one field within a view having this attribute set to 1 (new as of 5.2)

    1. <field name="name" default_focus=”1”/>

例子

Here’s the source code of the view of a sale order object. This is the same object as the object shown on the screen shots of the presentation.

Example:
  1. <?xml version="1.0"?>
  2. <openerp>
  3. <data>
  4. <record id="view_partner_form" model="ir.ui.view">
  5. <field name="name">res.partner.form</field>
  6. <field name="model">res.partner</field>
  7. <field name="type">form</field>
  8. <field name="arch" type="xml">
  9. <form string="Partners">
  10. <group colspan="4" col="6">
  11. <field name="name" select="1"/>
  12. <field name="ref" select="1"/>
  13. <field name="customer" select="1"/>
  14. <field domain="[('domain', '=', 'partner')]" name="title"/>
  15. <field name="lang" select="2"/>
  16. <field name="supplier" select="2"/>
  17. </group>
  18. <notebook colspan="4">
  19. <page string="General">
  20. <field colspan="4" mode="form,tree" name="address"
  21. nolabel="1" select="1">
  22. <form string="Partner Contacts">
  23. <field name="name" select="2"/>
  24. <field domain="[('domain', '=', 'contact')]" name="title"/>
  25. <field name="function"/>
  26. <field name="type" select="2"/>
  27. <field name="street" select="2"/>
  28. <field name="street2"/>
  29. <newline/>
  30. <field name="zip" select="2"/>
  31. <field name="city" select="2"/>
  32. <newline/>
  33. <field completion="1" name="country_id" select="2"/>
  34. <field name="state_id" select="2"/>
  35. <newline/>
  36. <field name="phone"/>
  37. <field name="fax"/>
  38. <newline/>
  39. <field name="mobile"/>
  40. <field name="email" select="2" widget="email"/>
  41. </form>
  42. <tree string="Partner Contacts">
  43. <field name="name"/>
  44. <field name="zip"/>
  45. <field name="city"/>
  46. <field name="country_id"/>
  47. <field name="phone"/>
  48. <field name="email"/>
  49. </tree>
  50. </field>
  51. <separator colspan="4" string="Categories"/>
  52. <field colspan="4" name="category_id" nolabel="1" select="2"/>
  53. </page>
  54. <page string="Sales &amp; Purchases">
  55. <separator string="General Information" colspan="4"/>
  56. <field name="user_id" select="2"/>
  57. <field name="active" select="2"/>
  58. <field name="website" widget="url"/>
  59. <field name="date" select="2"/>
  60. <field name="parent_id"/>
  61. <newline/>
  62. </page>
  63. <page string="History">
  64. <field colspan="4" name="events" nolabel="1" widget="one2many_list"/>
  65. </page>
  66. <page string="Notes">
  67. <field colspan="4" name="comment" nolabel="1"/>
  68. </page>
  69. </notebook>
  70. </form>
  71. </field>
  72. </record>
  73. <menuitem
  74. action="action_partner_form"
  75. id="menu_partner_form"
  76. parent="base.menu_base_partner"
  77. sequence="2"/>
  78. </data>
  79. </openerp>

The eval attribute

The eval attribute evaluate its content as if it was Python code. This allows you to define values that are not strings.

一般情况下,内容中标签一直作为字符串来计算。

Example 1:

  1. <field name="value">2.3</field>

2.3不是float 2.3而是字符串2.3

Example 2:

  1. <field name="value">False</field>

This will evaluate to the string ‘False’ and not the boolean False

如果你想要计算float,boolean或是其他类型,除了string,可以使用eval属性:

  1. <field name="value" eval="2.3" />
  2. <field name="value" eval="False" />

Button

加button到当前视图中。允许用户在当前记录上执行各种动作。

点击button按钮后,记录会重新装载。

Buttons有以下的属性:

@type

定义button按钮激活后action执行类型。

  • workflow (default)

    button会在当前model上发送workflow signal,使用button的name作为workflow signal name并且提供记录id作为参数(in a list)。

    The workflow signal may return an action descriptor, which should be executed. Otherwise it will return False.

    object

    The button will execute the method of name @name on the current model, providing the record id as parameter (in a list). This call may return an action descriptor to execute.

    action

    button会触发action(ir.actions.actions)来执行。这个动作的id是button的@name。

    From there, follows the normal action-execution workflow.

@special

当前仅有一个可能值:cancel,popup在没有RPC调用或是action执行时就会关闭。

注解

Only meaningful within a popup-type window (e.g. a wizard). Otherwise, is a noop.

警告

@special and @type are incompatible.

@name

button的标识符,用于指出调用哪个方法,发送哪个signal,执行哪个动作。

@confirm

当执行button的任务前,确认对话框会显示。当确认取消后,button的任务不会执行。

@string

button上会显示的标签。 [2].

@icon

显示button图表,如果没有这个button就是text-only [3].

@states, @attrs, @invisible

OpenERP这些视图属性的标准含义。

@default_focus

如果设置为确认值(1)时,当点击RETURN时就会自动搜索按钮。

client端会忽视这个属性。

6.0 新版功能.

Example:
  1. <button name="order_confirm" states="draft" string="Confirm Order" icon="gtk-execute"/>
  2. <button name="_action_open_window" string="Open Margins" type="object" default_focus=”1”/>

Label

用label来加个标题。

Example:
  1. <label string="Test"/>

New Line

如果视图的所有列不能全部放进一行是强制换行。

Example:
  1. <newline/>
[1]

via exec_workflow on the object rpc endpoint

[2]

in form view, in list view buttons have no label

[3]

behavior in list view is undefined, as list view buttons don’t have labels.