", { 'class': "icon icon-search" }))
17 | var divSearch = $("", { 'class': "row-fluid input-append" });
18 | divSearch.append(inputSearch).append(spanSearch);
19 | $(table).before(divSearch);
20 | var $rows = $(table).find('tbody tr' + defaultOptions.filterTrClass);
21 | inputSearch.keyup(function () {
22 | if (typeof defaultOptions.before === 'function')
23 | defaultOptions.before();
24 | var val = '^(?=.*' + $.trim($(this).val()).split(/\s+/).join(')(?=.*') + ').*$',
25 | reg = RegExp(val, 'i'),
26 | text;
27 | $rows.show().filter(function () {
28 | text = $(this).text().replace(/\s+/g, ' ');
29 | return !reg.test(text);
30 | }).hide();
31 | });
32 | }
33 | }
34 | $.fn.searchTable = function (options) {
35 | var st = new searchTable(this, options);
36 | st.setUp();
37 | }
38 | })(jQuery);
39 |
--------------------------------------------------------------------------------
/vanilla-search-table.js:
--------------------------------------------------------------------------------
1 | (function (wind) {
2 | 'use strict';
3 |
4 | class searchTable {
5 | constructor(table, options) {
6 | function setUpOptions(options) {
7 | const placeHolderConst = "Enter a search term";
8 | var defaultOptions = options || { placeholder: placeHolderConst, filterTrClass: "" };
9 | if (defaultOptions.placeholder === undefined)
10 | defaultOptions.placeholder = placeHolderConst;
11 | if (defaultOptions.filterTrClass === undefined)
12 | defaultOptions.filterTrClass = "";
13 | return defaultOptions;
14 | }
15 |
16 | this.setUp = function () {
17 | var defaultOptions = setUpOptions(options);
18 | var inputSearch = document.createElement('input');
19 | inputSearch.setAttribute('type', 'text');
20 | inputSearch.setAttribute('placeholder', defaultOptions.placeholder);
21 | inputSearch.classList.add('class', 'span6');
22 |
23 | var spanSearch = document.createElement('span')
24 | spanSearch.setAttribute('class', 'add-on')
25 | const icon = document.createElement('i');
26 | ['icon', 'icon-search'].forEach(cl => icon.classList.add('class', cl));
27 | spanSearch.appendChild(icon);
28 |
29 | const divSearch = document.createElement('div');
30 | ['row-fluid', 'input-append'].forEach(cl => divSearch.classList.add('class', cl));
31 | divSearch.appendChild(inputSearch);
32 | divSearch.append(spanSearch);
33 |
34 | document.querySelector(table).closest('div').insertBefore(divSearch, document.querySelector(table));
35 |
36 | var rows = document.querySelector(table).querySelectorAll('tbody tr' + defaultOptions.filterTrClass);
37 |
38 | inputSearch.addEventListener('keyup', function () {
39 | const val = '^(?=.*' + this.value.trim().split(/\s+/).join(')(?=.*') + ').*$';
40 | const reg = RegExp(val, 'i');
41 |
42 | [].forEach.call(rows, (el) => {
43 | const text = el.innerText.replace(/\s+/g, ' ');
44 | console.log('row el', text);
45 | if (reg.test(text))
46 | el.style.display = 'table-row';
47 | else
48 | el.style.display = 'none';
49 | });
50 | });
51 | };
52 | }
53 | }
54 |
55 | wind.searchTable = function (tableId, options) {
56 | console.log('document.searchTable', tableId);
57 | var st = new searchTable(tableId, options);
58 | st.setUp();
59 | };
60 | })(window);
61 |
--------------------------------------------------------------------------------