How it works...

In the case of name, we simply listed the field as the one to be offered to the user to search for. We left the semantics at the default, which is a substring search for character fields.

For categories, we do something more interesting. By default, your search term is applied to a many2many field triggers, name_search, which would be a substring search in the category names in this case. However, depending on your category structure, it can be very convenient to search for partners who have the category you're interested in or a child of it. Think about a main category, Newsletter subscribers, with the subcategories, Weekly newsletter, Monthly newsletter, and a couple of other newsletter types. Searching for newsletter subscribers with the preceding search view definition will give you everyone who is subscribed to any of those newsletters in one go, which is a lot more convenient than searching for every single type and combining the results.

The filter_domain attribute can contain an arbitrary domain, so you're neither restricted to searching for the same field you named in the name attribute, nor to using only one term. The self variable is what the user filled in, and also the only variable that you can use here.

Here's a more elaborate example from the default search view for partners:

<field name="name"
filter_domain="[
'|', '|',
('display_name', 'ilike', self),
('ref', '=', self),
('email', 'ilike', self)]"/>

This means that the user doesn't have to think about what to search for. All they need to do is type in some letters, press Enter, and, with a bit of luck, one of the fields mentioned contains the string we're looking for.

For the bank_ids field, we used another trick. The type of field not only decides the default way of searching for the user's input, but it also defines the way in which Odoo presents the suggestions. Also, given that many2one fields are the only ones that offer auto-completion, we force Odoo to do that, even though bank_ids is a one2many field, by setting the widget attribute. Without this, we will have to search in this field, without completion suggestions. The same applies to many2many fields. Note that every field with a many2one widget set will trigger a search on its model for every one of the user's keystrokes; don't use too many of them.

You should also put the most-used fields on the top, because the first field is what is searched if the user just types something and presses Enter. The search bar can also be used with the keyboard; select a suggestion by pressing the down arrow, and open the completion suggestion of a many2one by pressing the right arrow. If you educate your users in this and pay attention to a sensible ordering of fields in the search view, this will be much more efficient than typing something first, grabbing the mouse, and selecting an option.

The filter element creates a button that adds the content of the filter's domain attribute to the search domain. You should add a logical internal name and a string attribute to describe the filter to your users.

The <group> tag is used to provide grouping option under Group by button. In this recipe we have added option to group record based on country_id field.

..................Content has been hidden....................

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