Skip to content

Commit b138dff

Browse files
committed
table_search: Adding clear button
1 parent cbb5ef7 commit b138dff

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

js/table_search.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,33 @@
55
var $ts = $(elem);
66
var tableId = $ts.next().attr('id');
77
var inputId = 'searchtable-' + i;
8-
$ts.before('<label for="' + inputId + '"><strong>Enter keyword to search </strong></label><input id="' + inputId + '" type="search" autosave="csrsearch" results="1" placeholder="search" name="s">');
9-
$('#' + inputId).keyup(function () {
10-
searchTable($(this).val(), tableId);
8+
var clearId = 'clear-' + inputId;
9+
$ts.before('<div style="position: relative; width: fit-content;"><label for="' + inputId + '"><strong>Enter keyword to search </strong></label><input id="' + inputId + '" autosave="csrsearch" results="1" placeholder="search" name="s"><a href="#" id="' + clearId + '" class="search-clear" style="display:none; position: absolute; right: 5px; top: 2px;"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 34 34" class="svg-icon x-skinny" width="10"><use fill="#000" xlink:href="/sites/default/files/svg/sprite_8.svg#x-skinny"></use></svg><span style="position:absolute; left:-10000px; top:auto; width:1px; height:1px; overflow:hidden;">Clear</span></a></div>');
10+
11+
var $input = $('#' + inputId);
12+
var $clear = $('#' + clearId);
13+
14+
$input.keyup(function (e) {
15+
var val = $(this).val();
16+
17+
// Check if Escape key was pressed
18+
if (e.key === 'Escape' || e.keyCode === 27) {
19+
$(this).val('');
20+
searchTable('', tableId);
21+
$clear.hide();
22+
return;
23+
}
24+
25+
searchTable(val, tableId);
26+
$clear.toggle(val.length > 0);
27+
});
28+
29+
$clear.click(function (e) {
30+
e.preventDefault();
31+
$input.val('');
32+
searchTable('', tableId);
33+
$clear.hide();
34+
$input.focus();
1135
});
1236

1337
if ( $( ".page-node-1485" ).length ) {
@@ -16,11 +40,13 @@
1640
$ts.prev('.cat_search').change(function () {
1741
var selection = this.value;
1842
if (selection) {
19-
$('#' + inputId).val(selection);
43+
$input.val(selection);
2044
searchTable(selection, tableId);
45+
$clear.show();
2146
} else {
22-
$('#' + inputId).val('');
47+
$input.val('');
2348
searchTable(selection, tableId);
49+
$clear.hide();
2450
}
2551
}).change();
2652
}

0 commit comments

Comments
 (0)