Skip to content

Commit aabb6b5

Browse files
authored
Fixed templates for column filtering (#348)
* Fixed templates for column filtering Original author: @kathibeepboop * Harden comparaison in templates
1 parent ef6c686 commit aabb6b5

File tree

5 files changed

+66
-28
lines changed

5 files changed

+66
-28
lines changed

src/Resources/views/Filter/select.html.twig

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<label>
2-
<select
3-
id="{{ datatable.setting('name') }}-column-{{ column.index }}"
4-
data-search-column-index="{{ column.index }}"
5-
>
6-
{% if column.filter.placeholder != null %}
2+
<select class="form-control" id="{{ datatable.name }}-column-{{ column.index }}" data-search-column-index="{{ column.index }}">
3+
{% if column.filter.placeholder is not same as(null) %}
74
<option class="placeholder">{{ column.filter.placeholder|trans }}</option>
85
{% endif %}
96
{% for key, choice in column.filter.choices %}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
$('#{{ datatable.setting('name') }}').on('stateLoaded.dt', function (e, settings, data) {
2-
$('#{{ datatable.setting('name') }}-column-{{ column.index }}').val(data.columns[{{ column.index }}].search.search) ;
1+
$('#{{ datatable.name }}').on('stateLoaded.dt', function (e, settings, data) {
2+
$('#{{ datatable.name }}-column-{{ column.index }}').val(data.columns[{{ column.index }}].search.search);
33
});
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
<label>
2-
<input id="{{ datatable.setting('name') }}-column-{{ column.index }}"
2+
<input class="form-control"
3+
id="{{ datatable.name }}-column-{{ column.index }}"
34
data-search-column-index="{{ column.index }}"
4-
{% if column.filter.placeholder != null %}
5-
placeholder="{{ column.filter.placeholder|trans }}"
6-
{% endif %}
7-
value="{{ column.searchValue }}">
8-
<script>
9-
document.getElementById('domains').addEventListener('restore', function (e, data) {
10-
console.log(data);
11-
});
12-
</script>
5+
placeholder="{{ column.filter.placeholder ?? '' }}"
6+
value="{{ searchValue ?? '' }}"
7+
/>
138
</label>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
$('#{{ datatable.setting('name') }}').on('stateLoaded.dt', function (e, settings, data) {
2-
$('#{{ datatable.setting('name') }}-column-{{ column.index }}').val(data.columns[{{ column.index }}].search.search) ;
1+
$('#{{ datatable.name }}').on('stateLoaded.dt', function (e, settings, data) {
2+
$('#{{ datatable.name }}-column-{{ column.index }}').val(data.columns[{{ column.index }}].search.search);
33
});
Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,75 @@
11
{% trans_default_domain datatable.translationDomain %}
22

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 %}">
44
<thead>
55
<tr>
66
{% for column in datatable.columns %}
77
<th>{{ column.label|trans }}</th>
88
{% endfor %}
99
</tr>
10-
{#% if datatable.option('searching') and datatable.setting('column_filter') in ['both', 'thead'] %}
10+
{% if datatable.option('searching') and columnFilter ?? '' in ['both', 'thead'] %}
1111
<tr class="datatable-filters">
1212
{% 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>
1416
{% endfor %}
1517
</tr>
16-
{% endif %#}
18+
{% endif %}
1719
</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'] %}
1924
<tfoot>
2025
<tr class="datatable-filters">
2126
{% 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>
2330
{% endfor %}
2431
</tr>
2532
</tfoot>
26-
{% endif %#}
27-
<tbody>
28-
</tbody>
33+
{% endif %}
2934
</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

Comments
 (0)