3

By default, Odoo comes with a bunch of custom filters that can be configured like 'contains', 'is set', 'is not set'. I want to know how to make a custom filter like it starts with. I am able to read documentation how to add more fields to search on, but not how to add more operations to search on.

What can I do to make this basic form of searching possible? Most sql backends have a basic LIKE matching built-in, how do I expose that to end-user in Odoo 11.

CZoellner
  • 13,553
  • 3
  • 25
  • 38
Lawrence Kok
  • 1,568
  • 11
  • 29
  • `contains` also works based on `like` terminology. You can use `contains` instead of `like`. – Mayur Jotaniya Dec 27 '19 at 05:44
  • sorry but contains doesn't give me the filter that I need, I really need starts with ends with support. contains doesn't allow me to narrow down my search like I want. – Lawrence Kok Dec 27 '19 at 09:40
  • 1
    That won't be easy, i've not looked into it yet, but i guess you have to do big changes to Odoo's core: Extending the ORM and the client JS code. Besides that, some extensions for the search could be very helpful. – CZoellner Dec 27 '19 at 10:33
  • 1
    [Here](https://github.com/OCA/server-tools/tree/5455dbc975aa8da8e293bc6aa2a56fe591601311/base_search_fuzzy) is an example for extending the ORM (a bit). And [this](https://github.com/OCA/web/tree/4eb3831e1524f8192aebdd495f5a94ebd6282483/web_search_with_and) example shows some client JS changes. Both examples are eventually helpful in understanding what you have to do, but don't solve your problem. – CZoellner Dec 27 '19 at 10:43

2 Answers2

3

In Odoo 11 in the Github repository under addons, you can find the web module, which hosts basic web-framework and related files. Under the web module, there is a file called search_filters.js

This file contains the widget for the extended search menu (the one that shows the custom filters). The way this widget works is that every base primitive (text, number, id) is registered with prefined operations.

In the case of text search, it has the standard contains, equals, is set, is not set commands. All of these constructs a normal domain filter with the standard operators (ilike, !=, =) etc. So in order to implement a starts with and ends with is adding a new entries, and modifying the javascript to generating the right domain filter e.g. =ilike=query% or =ilike=%query

Lawrence Kok
  • 1,568
  • 11
  • 29
  • 1
    In Odoo 12/Odoo13 these structures seem to have changed, slightly. At least I couldn't see the same file `search_filters.js` anymore in the repository. – Lawrence Kok Jan 01 '20 at 05:18
  • In Odoo 13 on Windows, I found it in addons\web\static\src\js\views\control_panel\search and it worked! Thanks! – Always Lucky Jul 04 '20 at 00:34
2

You can use filter with a wildcard like the following for starts with / ends with:

  • starts with:

    ['field', '=like', 'letter%']
    
  • ends with:

    ['field', '=like', '%letter']
    
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
imad
  • 123
  • 8
  • 1
    I know about those operation exists, what I want to know is how to register those operations into the odoo front-end. It currently only has: contains, doesn't contains, is equal to, is not equal to, is set, is not set. If possible I'd like to get starts with, and ends with supported in that same list. – Lawrence Kok Dec 27 '19 at 09:39
  • 1
    I found where odoo brings in these operators and exposes them to the frontend: https://github.com/odoo/odoo/blob/11.0/addons/web/static/src/js/chrome/search_filters.js#L163 This seems to suggest contains already is using ilike operator for comparision but yet the search is '%dog%'. – Lawrence Kok Dec 29 '19 at 12:49