Search views

The search options available on views are defined with a search view. It defines the fields to be searched when typing in the search box It also provides predefined filters that can be activated with a click, and data grouping options for the records on list and kanban views.

Here is a search view for the to-do tasks:

<record id="todo_app.view_filter_todo_task"
        model="ir.ui.view">
  <field name="name">To-do Task Filter</field>
  <field name="model">todo.task</field>
  <field name="arch" type="xml">
    <search>
      <field name="name" domain_filter="['|',
          ('name','ilike',self),('user_id','ilike',self)]"/>
      <field name="user_id"/>
      <filter name="filter_not_done" string="Not Done"
              domain="[('is_done','=',False)]"/>
      <filter name="filter_done" string="Done"
              domain="[('is_done','!=',False)]"/>
      <separator/>
      <filter name="group_user" string="By User"
              context="{'group_by': 'user_id'}"/>
    </search>
  </field>
</record>

We can see two fields to be searched for: name and user_id. On name we have a custom filter rule that makes the "if search" both on the description and on the responsible user. Then we have two predefined filters, filtering the not done and done tasks. These filters can be activated independently, and will be joined with an "OR" operator if both are enabled. Blocks of filters separated with a <separator/> element will be joined with an "AND" operator.

The third filter only sets a group-by context. This tells the view to group the records by that field, user_id in this case.

The field elements can use these attributes:

  • name: identifies the field to use.
  • string: provides a label text to use instead of the default.
  • operator: allows us to use a different operator other than the default – "=" for numeric fields and ilike for the other field types.
  • filter_domain: can be used to set a specific domain expression to use for the search, providing much more flexibility than the operator attribute. The text being searched for is referenced in the expression using self.
  • groups: makes the search on the field available only for a list of security groups (identified by XML IDs).

For the filter elements these are the attributes available:

  • name: is an identifier to use for inheritance or for enabling it through search_default_ keys in the context of window actions.
  • string: provides label text to display for the filter (required).
  • domain: provides the filter domain expression to be added to the active domain.
  • context: is a context dictionary to add to the current context. Usually this sets a group_by key with the name of the field to group the records.
  • groups: makes the search filter available only for a list of groups.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset