### 搜索視圖
搜索查看` <字段> `元素可以有一個` @ filter_domain `,覆蓋在特定領域搜索生成的域。在給定的域中,“自我”代表用戶輸入的值。在下面的例子中,它是用來搜索兩個字段的名稱和描述。
搜索視圖也可以包含` <filter> `元素,從而作為預定義的搜索切換。過濾器必須具有下列屬性之一:
`domain`
將給定的域添加到當前搜索
`context`
添加一些背景下,當前的搜索;使用的關鍵` group_by `在給定的字段名稱組結果
~~~ xml
<search string="Ideas">
<field name="name"/>
<field name="description" string="Name and description"
filter_domain="['|', ('name', 'ilike', self), ('description', 'ilike', self)]"/>
<field name="inventor_id"/>
<field name="country_id" widget="selection"/>
<filter name="my_ideas" string="My Ideas"
domain="[('inventor_id', '=', uid)]"/>
<group string="Group By">
<filter name="group_by_inventor" string="Inventor"
context="{'group_by': 'inventor_id'}"/>
</group>
</search>
~~~
在一個動作中使用非默認搜索視圖,它應與使用的行動記錄` search_view_id `場。
行動也可以設置默認值為搜索領域通過其`context`領域:形式的` search_default_ * field_name * `將初始化提供的價值 *field_name* 上下文鍵。搜索過濾器必須有一個可選的`@name`有違約,表現為布爾值(他們只能默認啟用)。
練習
搜索視圖
1. 添加一個按鈕來過濾當前用戶在搜索視圖中負責的課程。默認選擇的。
2. 由負責任的用戶添加一個按鈕組課程。
*openacademy/views/openacademy.xml*
~~~ xml
<search>
<field name="name"/>
<field name="description"/>
<filter name="my_courses" string="My Courses"
domain="[('responsible_id', '=', uid)]"/>
<group string="Group By">
<filter name="by_responsible" string="Responsible"
context="{'group_by': 'responsible_id'}"/>
</group>
</search>
</field>
</record>
~~~
~~~ xml
<field name="res_model">openacademy.course</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="context" eval="{'search_default_my_courses': 1}"/>
<field name="help" type="html">
<p class="oe_view_nocontent_create">Create the first course
</p>
~~~