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/5.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

  1. <group expand="1" string="Group By...">
  2. <filter string="Users" icon="terp-project" domain="[]" context="{'group_by':'user_id'}"/>
  3. <filter string="Project" icon="terp-project" domain="[]" context="{'group_by':'project_id'}"/>
  4. <separator orientation="vertical"/>
  5. <filter string="Deadline" icon="terp-project" domain="[]" context="{'group_by':'date_deadline'}"/>
  6. </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.

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

Group By

  1. <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是可能的。这种情况下,嵌套分组就会显示出来:

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

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是指的名字(其实对于filters,name属性并不是必须的,当它必须要有个明确的name值时才起作用)。这个值要么是search fields的初始值,要么是filter的布尔值,现在来切换它们:

  1. <record id="action_view_task" model="ir.actions.act_window">
  2. <field name="name">Tasks</field>
  3. <field name="res_model">project.task</field>
  4. <field name="view_type">form</field>
  5. <field name="view_mode">tree,form,calendar,gantt,graph</field>
  6. <field eval="False" name="filter"/>
  7. <field name="view_id" ref="view_task_tree2"/>
  8. <field name="context">{"search_default_current":1,"search_default_user_id":uid}</field>
  9. <field name="search_view_id" ref="view_task_search_form"/>
  10. </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/5.0/_images/filter.png

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

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