Odoo 11 Development Essentials(Third Edition)
上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 todo_view.xml:

<record id="view_tree_todo_task" model="ir.ui.view"> 
  <field name="name">To-do Task Tree</field> 
  <field name="model">todo.task</field> 
  <field name="arch" type="xml"> 
    <tree colors="decoration-muted:is_done==True"> 
      <field name="name"/> 
      <field name="is_done"/> 
    </tree> 
  </field> 
</record> 

This defines a list with only two columns: name and is_done. We also added a nice touch: the lines for done tasks (is_done==True) are shown as grayed out. This is done by applying the muted Bootstrap class. Check http://getbootstrap.com/docs/3.3/css/#helper-classes-colors for more information on Bootstrap and its contextual colors.

Because is_done is a Boolean field, the "decoration-muted:is_done==True" can elegantly be simplified to "decoration-muted:is_done". Try it!

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 todo_view.xml:

<record id="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="user_id"/> 
      <filter string="Not Done" 
        domain="[('is_done','=',False)]"/> 
      <filter string="Done" 
        domain="[('is_done','!=',False)]"/> 
    </search> 
  </field> 
</record> 

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