|
| 1 | +Quick filters for searches |
| 2 | +========================== |
| 3 | + |
| 4 | +Quick filters provide an efficient way to search using existing search commands. This documentation outlines how to create and implement new quick filters for Mautic searches. |
| 5 | + |
| 6 | +Implementation overview |
| 7 | +----------------------- |
| 8 | + |
| 9 | +Quick filters are implemented using a combination of JavaScript and Twig templates. The process involves three main components: |
| 10 | + |
| 11 | +1. JavaScript function for applying the filter |
| 12 | +2. Twig template for rendering filter buttons |
| 13 | +3. Array of PHP code for defining filter options |
| 14 | + |
| 15 | +JavaScript functionality |
| 16 | +------------------------ |
| 17 | + |
| 18 | +The ``Mautic.listQuickFilter`` function is responsible for applying the quick filter: |
| 19 | + |
| 20 | +.. code-block:: javascript |
| 21 | +
|
| 22 | + Mautic.listQuickFilter = function (element) { |
| 23 | + const filterValue = element.dataset.filter; |
| 24 | + const searchInput = document.getElementById('list-search'); |
| 25 | + searchInput.value = filterValue; |
| 26 | + const enterKeyEvent = new KeyboardEvent('keyup', { |
| 27 | + keyCode: 13 |
| 28 | + }); |
| 29 | + searchInput.dispatchEvent(enterKeyEvent); |
| 30 | + } |
| 31 | +
|
| 32 | +This function performs the following actions: |
| 33 | + |
| 34 | +1. Retrieves the filter value from the clicked element's ``data-filter`` attribute |
| 35 | +2. Sets the search input field's value to the filter value |
| 36 | +3. Simulates an Enter key press to trigger the search |
| 37 | + |
| 38 | +Twig template |
| 39 | +------------- |
| 40 | + |
| 41 | +The quick filter buttons are rendered using a Twig template: |
| 42 | + |
| 43 | +.. code-block:: twig |
| 44 | +
|
| 45 | + {% if quickFilters is defined and quickFilters is not empty %} |
| 46 | + <div class="d-flex gap-xs"> |
| 47 | + {% for quickFilter in quickFilters %} |
| 48 | + <a class="label label-outline" |
| 49 | + data-filter="{{ quickFilter.search }}" |
| 50 | + onclick="Mautic.listQuickFilter(this)" |
| 51 | + data-toggle="tooltip" |
| 52 | + data-placement="top" |
| 53 | + data-original-title="{{ quickFilter.tooltip|trans }}"> |
| 54 | + <i class="{{ quickFilter.icon }}"></i> |
| 55 | + {{ quickFilter.label|trans }} |
| 56 | + </a> |
| 57 | + {% endfor %} |
| 58 | + </div> |
| 59 | + {% endif %} |
| 60 | +
|
| 61 | +This template iterates through the provided quick filters and creates clickable labels for each one on the toolbar. |
| 62 | + |
| 63 | +Implementing quick filters |
| 64 | +-------------------------- |
| 65 | + |
| 66 | +To add quick filters to a list view, include the ``list_toolbar.html.twig`` template and pass the ``quickFilters`` option: |
| 67 | + |
| 68 | +.. code-block:: twig |
| 69 | +
|
| 70 | + {{ include('@MauticCore/Helper/list_toolbar.html.twig', { |
| 71 | + 'searchValue': searchValue, |
| 72 | + 'searchHelp': 'mautic.form.form.help.searchcommands', |
| 73 | + 'searchId': 'list-search', |
| 74 | + 'action': currentRoute, |
| 75 | + 'quickFilters': [ |
| 76 | + { |
| 77 | + 'search': 'has:results', |
| 78 | + 'label': 'mautic.core.search.quickfilter.form_results', |
| 79 | + 'tooltip': 'mautic.core.search.quickfilter.form_results.tooltip', |
| 80 | + 'icon': 'ri-file-list-2-line' |
| 81 | + } |
| 82 | + ] |
| 83 | + }) }} |
| 84 | +
|
| 85 | +Quick filter options |
| 86 | +-------------------- |
| 87 | + |
| 88 | +Each quick filter is defined as an associative array with the following keys: |
| 89 | + |
| 90 | +- ``search``: The search query to be applied |
| 91 | +- ``label``: The displayed text for the filter button |
| 92 | +- ``tooltip``: The tooltip text shown on hover |
| 93 | +- ``icon``: The CSS class for the icon displayed on the button |
| 94 | + |
| 95 | +Multiple quick filters |
| 96 | +---------------------- |
| 97 | + |
| 98 | +You can define multiple quick filters by adding more items to the ``quickFilters`` array: |
| 99 | + |
| 100 | +.. code-block:: php |
| 101 | +
|
| 102 | + 'quickFilters': [ |
| 103 | + { |
| 104 | + 'search': 'is:admin', |
| 105 | + 'label': 'mautic.user.role.form.isadmin', |
| 106 | + 'tooltip': 'mautic.core.search.quickfilter.is_admin', |
| 107 | + 'icon': 'ri-admin-line' |
| 108 | + }, |
| 109 | + { |
| 110 | + 'search': 'is:published', |
| 111 | + 'label': 'mautic.core.form.active', |
| 112 | + 'tooltip': 'mautic.core.search.quickfilter.is_published', |
| 113 | + 'icon': 'ri-check-line' |
| 114 | + }, |
| 115 | + { |
| 116 | + 'search': 'is:unpublished', |
| 117 | + 'label': 'mautic.core.form.inactive', |
| 118 | + 'tooltip': 'mautic.core.search.quickfilter.is_unpublished', |
| 119 | + 'icon': 'ri-close-line' |
| 120 | + } |
| 121 | + ] |
0 commit comments