|
1 | 1 | {% trans_default_domain datatable.translationDomain %} |
2 | 2 |
|
3 | | -<table id="{{ datatable.name }}" class="{% if className is defined and className is not empty %}{{ className }}{% endif %}"> |
| 3 | +<table id="{{ datatable.name }}" class="{% if className is defined and className is not empty %}{{ className }}{% endif %}"> |
4 | 4 | <thead> |
5 | 5 | <tr> |
6 | 6 | {% for column in datatable.columns %} |
7 | 7 | <th>{{ column.label|trans }}</th> |
8 | 8 | {% endfor %} |
9 | 9 | </tr> |
10 | | - {#% if datatable.option('searching') and datatable.setting('column_filter') in ['both', 'thead'] %} |
| 10 | + {% if datatable.option('searching') and columnFilter ?? '' in ['both', 'thead'] %} |
11 | 11 | <tr class="datatable-filters"> |
12 | 12 | {% for column in datatable.columns %} |
13 | | - <td>{% if column.filter != null %}{% include column.filter.templateHtml %}{% endif %}</td> |
| 13 | + <td> |
| 14 | + {% if column.filter is not same as(null) %}{% include column.filter.templateHtml %}{% endif %} |
| 15 | + </td> |
14 | 16 | {% endfor %} |
15 | 17 | </tr> |
16 | | - {% endif %#} |
| 18 | + {% endif %} |
17 | 19 | </thead> |
18 | | - {#% if datatable.option('searching') and datatable.setting('column_filter') in ['both', 'tfoot'] %} |
| 20 | + <tbody> |
| 21 | + </tbody> |
| 22 | + |
| 23 | + {% if datatable.option('searching') and columnFilter ?? '' in ['both', 'tfoot'] %} |
19 | 24 | <tfoot> |
20 | 25 | <tr class="datatable-filters"> |
21 | 26 | {% for column in datatable.columns %} |
22 | | - <td>{% if column.filter != null %}{% include column.filter.templateHtml %}{% endif %}</td> |
| 27 | + <td> |
| 28 | + {% if column.filter is not same as(null) %}{% include column.filter.templateHtml %}{% endif %} |
| 29 | + </td> |
23 | 30 | {% endfor %} |
24 | 31 | </tr> |
25 | 32 | </tfoot> |
26 | | - {% endif %#} |
27 | | - <tbody> |
28 | | - </tbody> |
| 33 | + {% endif %} |
29 | 34 | </table> |
| 35 | + |
| 36 | +{% if datatable.option('searching') %} |
| 37 | + <script> |
| 38 | + $('#{{ datatable.name }}').on('init.dt', function (e, settings, json) { |
| 39 | + const table = $('#{{ datatable.name }}').DataTable(); |
| 40 | +
|
| 41 | + {% for column in datatable.columns %} |
| 42 | + {% if column.filter is not same as(null) %} |
| 43 | + {% include column.filter.templateJs %} |
| 44 | +
|
| 45 | + $(function () { |
| 46 | + $('#{{ datatable.name }}-column-{{ column.index }}').on( |
| 47 | + "keyup change", |
| 48 | + delay(function () { |
| 49 | + const column = table.columns({{ column.index }}); |
| 50 | + const newValue = $(this).val(); |
| 51 | +
|
| 52 | + if (column.search() !== newValue) { |
| 53 | + column.search(newValue).draw(); |
| 54 | + } |
| 55 | + } |
| 56 | + , {{ datatable.option('searchDelay') }} |
| 57 | + ) |
| 58 | + ) |
| 59 | + }); |
| 60 | + {% endif %} |
| 61 | + {% endfor %} |
| 62 | + }); |
| 63 | +
|
| 64 | + function delay(callback, ms) { |
| 65 | + let timer = 0; |
| 66 | + return function () { |
| 67 | + let context = this, args = arguments; |
| 68 | + clearTimeout(timer); |
| 69 | + timer = setTimeout(function () { |
| 70 | + callback.apply(context, args); |
| 71 | + }, ms || 0); |
| 72 | + }; |
| 73 | + } |
| 74 | + </script> |
| 75 | +{% endif %} |
0 commit comments