|
1 | 1 | import * as List from "list.js"; |
2 | 2 |
|
| 3 | +// function set_width() { |
| 4 | +// w = $(this) |
| 5 | +// .children("tr:first") |
| 6 | +// .children("th, td") |
| 7 | +// .map(function () { |
| 8 | +// return $(this) |
| 9 | +// .css("width"); |
| 10 | +// }); |
| 11 | +// console.log(w); |
| 12 | + |
| 13 | +// $(tbody) |
| 14 | +// .children("tr:first") |
| 15 | +// .children("th, td") |
| 16 | +// .each(function (i) { |
| 17 | +// console.log(i, w[i]); |
| 18 | +// $(this) |
| 19 | +// .css("width", w[i]); |
| 20 | +// }); |
| 21 | +// } |
| 22 | + |
| 23 | +// FIXME sort only works on first table |
| 24 | + |
| 25 | +var table_cnt = 0; |
| 26 | + |
3 | 27 | $(document) |
4 | 28 | .ready(function () { |
5 | 29 | $("table.tablesorter") |
6 | 30 | .each(function () { |
7 | | - var searcher = $.parseHTML(` |
8 | | - <div class="input-group mb-3"> |
| 31 | + var header_row = $(this) |
| 32 | + .find("thead > tr:first"); |
| 33 | + |
| 34 | + var fields = $(header_row) |
| 35 | + .find("*") |
| 36 | + .map(function () { |
| 37 | + return $(this) |
| 38 | + .attr("data-sort"); |
| 39 | + }) |
| 40 | + .toArray(); |
| 41 | + |
| 42 | + if (fields.length == 0) { |
| 43 | + console.log("No table fields defined, disabling search/sort."); |
| 44 | + |
| 45 | + } else { |
| 46 | + |
| 47 | + $(header_row) |
| 48 | + .children("[data-sort]") |
| 49 | + .addClass("sort"); |
| 50 | + |
| 51 | + if ($(header_row) |
| 52 | + .text() |
| 53 | + .trim() == "") { |
| 54 | + console.log("No headers fields visible, hiding header row."); |
| 55 | + header_row.addClass("visually-hidden"); |
| 56 | + } |
| 57 | + |
| 58 | + var searcher = $.parseHTML(` |
| 59 | + <div class="input-group my-3"> |
9 | 60 | <input type="search" class="search form-control" placeholder="Search"/> |
10 | 61 | <button class="btn btn-outline-secondary search-reset" type="button"> |
11 | 62 | <i class="bi bi-x"></i> |
12 | 63 | </button> |
13 | 64 | </div>`); |
14 | 65 |
|
15 | | - $(this) |
16 | | - .before(searcher); |
| 66 | + $(this) |
| 67 | + .before(searcher); |
17 | 68 |
|
18 | | - var fields = $(this) |
19 | | - .find("thead > tr:first") |
20 | | - .children("th") |
21 | | - .map(function () { |
22 | | - return $(this) |
23 | | - .attr("data-field"); |
24 | | - }) |
25 | | - .toArray(); |
26 | | - console.log(fields); |
| 69 | + var search_field = $(searcher) |
| 70 | + .children("input.search"); |
27 | 71 |
|
28 | | - var search_field = $(searcher) |
29 | | - .find("input.search"); |
| 72 | + var reset_search = $(searcher) |
| 73 | + .children("button.search-reset"); |
30 | 74 |
|
31 | | - var reset_search = $(searcher) |
32 | | - .find("button.search-reset"); |
| 75 | + var instance = []; |
33 | 76 |
|
34 | | - if (fields.length == 0) { |
35 | | - searcher.addClass("visually-hidden"); |
36 | | - } else { |
37 | | - console.log($(this)[0]); |
38 | | - var list = new List($(this) |
39 | | - .parent()[0], { valueNames: fields }); |
| 77 | + var first_table; |
| 78 | + var last_table; |
| 79 | + $(this) |
| 80 | + .children("tbody") |
| 81 | + .addClass("list") |
| 82 | + .each(function () { |
| 83 | + var parent; |
| 84 | + if (first_table === undefined) { |
| 85 | + first_table = $(this) |
| 86 | + .parent(); |
| 87 | + last_table = first_table; |
| 88 | + parent = first_table[0]; |
| 89 | + } else { |
| 90 | + var new_table = $(first_table) |
| 91 | + .clone() |
| 92 | + .empty() |
| 93 | + .removeClass("tablesorter"); |
| 94 | + $(last_table) |
| 95 | + .after(new_table); |
| 96 | + var thead = $(this) |
| 97 | + .prev("thead") |
| 98 | + .detach(); |
| 99 | + var tbody = $(this) |
| 100 | + .detach(); |
| 101 | + new_table.append(thead, tbody); |
| 102 | + parent = $(new_table)[0]; |
| 103 | + last_table = new_table; |
| 104 | + } |
| 105 | + |
| 106 | + $(parent) |
| 107 | + .addClass("tablesorter-table-" + table_cnt); |
| 108 | + |
| 109 | + instance.push( |
| 110 | + new List(parent, { valueNames: fields })); |
| 111 | + }); |
| 112 | + |
| 113 | + table_cnt++; |
40 | 114 |
|
41 | 115 | reset_search.on("click", function () { |
42 | 116 | search_field.val(""); |
43 | | - list.search(); |
| 117 | + $.each(instance, (i, e) => { |
| 118 | + e.search(); |
| 119 | + }); |
44 | 120 | }); |
45 | 121 |
|
46 | | - search_field.on("keydown", function (e) { |
47 | | - if (e.key == "Escape") { |
| 122 | + search_field.on("keyup", function (event) { |
| 123 | + if (event.key == "Escape") { |
48 | 124 | reset_search.trigger("click"); |
| 125 | + } else { |
| 126 | + $.each(instance, (i, e) => { |
| 127 | + e.search($(this) |
| 128 | + .val()); |
| 129 | + }); |
49 | 130 | } |
50 | 131 | }); |
51 | 132 |
|
52 | | - list.on("searchComplete", function () { |
53 | | - var last_show_with_children = -1; |
54 | | - for (var i = 0; i < list.items.length; i++) { |
55 | | - if ($(list.items[i].elm) |
56 | | - .hasClass("show-with-children")) { |
57 | | - last_show_with_children = i; |
58 | | - } |
| 133 | + $.each(instance, (i, e) => { |
| 134 | + e.on("searchComplete", function () { |
| 135 | + var last_show_with_children = {}; |
| 136 | + e.items.forEach((item) => { |
| 137 | + if ($(item.elm) |
| 138 | + .hasClass("show-with-children")) { |
| 139 | + var kind = $(item.elm) |
| 140 | + .attr("class") |
| 141 | + .split(/\s+/) |
| 142 | + .join(); |
| 143 | + last_show_with_children[kind] = item; |
| 144 | + } |
59 | 145 |
|
60 | | - if (list.items[i].found && |
61 | | - last_show_with_children >= 0 && |
62 | | - list.items[last_show_with_children].found == false) { |
63 | | - list.items[last_show_with_children].found = true; |
64 | | - list.items[last_show_with_children].show(); |
65 | | - last_show_with_children = -1; |
66 | | - } |
| 146 | + if (item.found) { |
| 147 | + Object.entries(last_show_with_children) |
| 148 | + .forEach(([key, val]) => { |
| 149 | + val.found = true; |
| 150 | + val.show(); |
| 151 | + delete last_show_with_children[key]; |
| 152 | + }); |
| 153 | + } |
67 | 154 |
|
68 | | - if ($(list.items[i].elm) |
69 | | - .hasClass("show-always")) { |
70 | | - list.items[i].found = true; |
71 | | - list.items[i].show(); |
72 | | - } |
73 | | - } |
74 | | - list.update(); |
| 155 | + if ($(item.elm) |
| 156 | + .hasClass("show-always")) { |
| 157 | + item.found = true; |
| 158 | + item.show(); |
| 159 | + } |
| 160 | + }); |
| 161 | + e.update(); |
| 162 | + }); |
75 | 163 | }); |
76 | 164 | } |
77 | 165 | }); |
|
0 commit comments