This is the documentation for older versions of Odoo (formerly OpenERP).

See the new Odoo user documentation.

See the new Odoo technical documentation.

Search views

Search views are a new feature of OpenERP supported as of version 6.0 It creates a customized search panel, and is declared quite similarly to a form view, except that the view type and root element change to search instead of form.

/doc_static/6.0/_images/search.png

下面是search视图中要用到的新的元素和特性列表。

Group tag

Unlike form group elements, search view groups support unlimited number of widget(fields or filters) in a row (no automatic line wrapping), and only use the following attributes:

  • expand: turns on the expander icon on the group (1 for expanded by default, 0 for collapsed)

  • string: label for the group

<group expand="1" string="Group By...">
   <filter string="Users" icon="terp-project" domain="[]" context="{'group_by':'user_id'}"/>
   <filter string="Project" icon="terp-project" domain="[]" context="{'group_by':'project_id'}"/>
   <separator orientation="vertical"/>
   <filter string="Deadline" icon="terp-project" domain="[]" context="{'group_by':'date_deadline'}"/>
</group>

以上的截图是个扩展group。

Filter tag

Filters在查找面板作为一个触发按钮方式显示,可以添加新的过滤元素在当前域或是查询视图的上下文。Filters可以作为一个字段子元素添加进去,来表示他们专门用于该字段(这种情况下button按钮会变小些)。

In the picture above the red area contains filters at the top of the form while the blue area highlights a field and it's child filter.

<filter string="Current" domain="[('state','in',('open','draft'))]" help="Draft, Open and Pending Tasks" icon="terp-project"/>
<field name="project_id" select="1" widget="selection">
    <filter domain="[('project_id.user_id','=',uid)]" help="My Projects" icon="terp-project"/>
</field>

Group By

<filter string="Project" icon="terp-project" domain="[]" context="{'group_by':'project_id'}"/>

以上的filters groups records都用相同的project_id值。groups懒加载,所以,inner records只在group扩展时加载。group header lines包含这个group中所有记录的共同值,并且所有数字型的字段一般先是在视图中,来代替值的和。

将指定的a list of fields的很多值分组来代替a single string是可能的。这种情况下,嵌套分组就会显示出来:

<filter string="Project" icon="terp-project" domain="[]" context="{'group_by': ['project_id', 'user_id'] }"/>

字段(Fields)

search view里面的字段元素用来为搜索得到user-provided值。其结果是,对于组元素(group elements),它们和表单视图的字段大不相同:

  • 一个搜索字段可以包含过滤(filters),通常表明字段和过滤管理相同的字段并且二者相关。

    那些内部的过滤器渲染为小buttons,紧挨着字段,肯定没有string属性。

  • 一个search字段确实构造了一个由[(field_name, operator, field_value)]构成的domain。这个domain在两种情况下无效:

    • @operator replaces the default operator for the field (which depends on its type)

    • @filter_domain lets you provide a fully custom domain, which will replace the default domain creation

  • 一个search字段不能默认创建上下文,但是你可以提供一个context,来评估和合并进wider context。

为了在你的context或filter_domain中获得字段值,你可以使用可变的self:

<field name="location_id" string="Location"
       filter_domain="['|',('location_id','ilike',self),('location_dest_id','ilike',self)]"/>

or

<field name="journal_id" widget="selection"
       context="{'journal_id':self, 'visible_id':self, 'normal_view':False}"/>

Range fields (date, datetime, time)

The range fields are composed of two input widgets (from and two) instead of just one.

这导致了它的特殊性(相比于non-range search fields来说):

  • 由于domain由两个section构成,每个section使用不同的operator,但通过@operator重写range field是不可能的。

  • Instead of being a simple value (integer, string, float) self for use in @filter_domain and @context is a dict.

    因为range field的每个input widget可以为空(并且字段本身始终有效),必须谨慎使用self:它有两个string keys“from”和“to”,这些keys任意一个可以不使用或设置其值为False。

Actions for Search view

在声明一个search view后,它会被自动用在相同model的所有tree view中。如果一个model有几个search view,那它会使用拥有最高优先级的那个。另一个选择就是通过设置action中的search_view_id字段来明确选择想要使用的search view。

除了能够传递在action上下文中的默认表单值外,OpenERP 6.0现在支持通过上下文来传递search views的初始值。这个上下文keys需要匹配search_default_XXX格式。在search view中XXX是指<field>或<filter>的名字(其实对于filters,name属性并不是必须的,当它必须要有个明确的name值时才起作用)。这个值要么是search fields的初始值,要么是filter的布尔值,现在来切换它们:

<record id="action_view_task" model="ir.actions.act_window">
    <field name="name">Tasks</field>
    <field name="res_model">project.task</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form,calendar,gantt,graph</field>
    <field eval="False" name="filter"/>
    <field name="view_id" ref="view_task_tree2"/>
    <field name="context">{"search_default_current":1,"search_default_user_id":uid}</field>
    <field name="search_view_id" ref="view_task_search_form"/>
</record>

Custom Filters

As of v6.0, all search views also features custom search filters, as show below. Users can define their own custom filters using any of the fields available on the current model, combining them with AND/OR operators. It is also possible to save any search context (the combination of all currently applied domain and context values) as a personal filter, which can be recalled at any time. Filters can also be turned into Shortcuts directly available in the User's homepage.

/doc_static/6.0/_images/filter.png

在以上的截图中,我们过滤Partner用Salesman = Demo user 和Country = Belgium,我们将这个搜索标准作为快捷键或是保存为过滤器。

过滤器是用户特定的,可以通过在filters 下拉菜单中管理过滤器选项来进行修改。