Odoo 12 Development Essentials
上QQ阅读APP看书,第一时间看更新

Adding list and search views

When viewing a model in list mode, a <tree> view is used. Tree views are capable of displaying lines organized in hierarchies, but most of the time, they are used to display plain lists.

We can add the following <tree> view definition to book_view.xml:

<record id="view_tree_book" model="ir.ui.view"> 
  <field name="name">Book List</field> 
  <field name="model">library.book</field> 
  <field name="arch" type="xml"> 
    <tree> 
      <field name="name"/> 
      <field name="author_ids" widget="many2many_tags" />
<field name="publisher_id"/> <field name="date_published"/>
    </tree> 
  </field> 
</record> 

This defines a list with four columns: name, author_idspublisher_id, and date_published.

At the top-right corner of the list, Odoo displays a search box. The fields it searches in and the available filters are defined by a <search> view.

As before, we will add this to book_view.xml:

<record id="view_search_book" model="ir.ui.view"> 
  <field name="name">Book Filters</field> 
  <field name="model">library.book</field> 
  <field name="arch" type="xml"> 
    <search> 
      <field name="publisher_id"/> 
      <filter name="filter_inactive"
string="Inactive"
domain="[('active','=',True)]"/>
<filter name="filter_active"
string="Active"
domain="[('active','=',False)]"/> </search>
</field> </record>

The <field> elements define fields that are also searched when typing in the search box. We added publisher_id to automatically suggest searching in the publisher field. The <filter> elements add predefined filter conditions, which can be toggled with a user click, are defined using a specific syntax. This will be addressed in more detail in Chapter 10, Backend Views – Design the User Interface.

Changed in Odoo 12
<filter> elements are now required to have a name="..." attribute, uniquely identifying each filter definition. If missing, the XML validation will fail and the module will not install or upgrade.