Debian Codegraph @

docs/example-widget-filter-custom.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>jQuery tablesorter 2.0 - Custom Filter Widget Functions</title>
6
7 <!-- jQuery -->
8 <script src="js/jquery-latest.min.js"></script>
9
10 <!-- Demo stuff -->
11 <link class="ui-theme" rel="stylesheet" href="css/jquery-ui.min.css">
12 <script src="js/jquery-ui.min.js"></script>
13 <link rel="stylesheet" href="css/jq.css">
14 <link href="css/prettify.css" rel="stylesheet">
15 <script src="js/prettify.js"></script>
16 <script src="js/docs.js"></script>
17
18 <!-- Tablesorter: required -->
19 <link rel="stylesheet" href="../css/theme.blue.css">
20 <script src="../js/jquery.tablesorter.js"></script>
21 <script src="../js/widgets/widget-storage.js"></script>
22 <script src="../js/widgets/widget-filter.js"></script>
23
24 <script>
25 $(function() {
26
27 // External search
28 // buttons set up like this:
29 // <button type="button" class="search" data-filter-column="4" data-filter-text="2?%">Saved Search</button>
30 $('button.match').click(function() {
31
32 // toggle "filter-match" class on first column
33 var first = $('table').find('th:first').toggleClass('filter-match');
34 $('#mode').html( '' + first.hasClass('filter-match') );
35
36 /*** first method *** data-filter-column="1" data-filter-text="!son"
37 add search value to Discount column (zero based index) input */
38 var filters = $('table').find('.tablesorter-filter'),
39 col = $(this).data('filter-column'), // zero-based index
40 txt = $(this).data('filter-text'); // text to add to filter
41
42 filters.val(''); // clear all filters
43 filters.eq(col).val(txt).trigger('search', false);
44 return false;
45 });
46
47 $('.accordion button[data-filter-column]').click(function() {
48 var filters = [],
49 $t = $(this),
50 col = $t.data('filter-column'), // zero-based index
51 txt = $t.data('filter-text') || $t.text(); // text to add to filter
52 filters[col] = txt;
53 $.tablesorter.setFilters( $('#demo table'), filters, true );
54 return false;
55 });
56
57 $('button.search-type').click(function() {
58 var c = $('#demo table')[0].config,
59 wo = c.widgetOptions,
60 search = wo.filter_searchFiltered = !wo.filter_searchFiltered;
61 $('#search-type').html( '' + search );
62 $.tablesorter.setFilters( c.$table, [ '', 'Evan' ], true );
63 });
64
65 });
66 </script>
67
68 <script id="js">$(function() {
69
70 // call the tablesorter plugin
71 $("table").tablesorter({
72 theme: 'blue',
73
74 // hidden filter input/selects will resize the columns, so try to minimize the change
75 widthFixed : true,
76
77 // initialize zebra striping and filter widgets
78 widgets: ["zebra", "filter"],
79
80 // headers: { 5: { sorter: false, filter: false } },
81
82 widgetOptions : {
83
84 // extra css class applied to the table row containing the filters & the inputs within that row
85 filter_cssFilter : '',
86
87 // If there are child rows in the table (rows with class name from "cssChildRow" option)
88 // and this option is true and a match is found anywhere in the child row, then it will make that row
89 // visible; default is false
90 filter_childRows : false,
91
92 // if true, filters are collapsed initially, but can be revealed by hovering over the grey bar immediately
93 // below the header row. Additionally, tabbing through the document will open the filter row when an input gets focus
94 filter_hideFilters : false,
95
96 // Set this option to false to make the searches case sensitive
97 filter_ignoreCase : true,
98
99 // jQuery selector string of an element used to reset the filters
100 filter_reset : '.reset',
101
102 // Use the $.tablesorter.storage utility to save the most recent filters
103 filter_saveFilters : true,
104
105 // Delay in milliseconds before the filter widget starts searching; This option prevents searching for
106 // every character while typing and should make searching large tables faster.
107 filter_searchDelay : 300,
108
109 // Set this option to true to use the filter to find text from the start of the column
110 // So typing in "a" will find "albert" but not "frank", both have a's; default is false
111 filter_startsWith : false,
112
113 // Add select box to 4th column (zero-based index)
114 // each option has an associated function that returns a boolean
115 // function variables:
116 // e = exact text from cell
117 // n = normalized value returned by the column parser
118 // f = search filter input value
119 // i = column index
120 filter_functions : {
121
122 // Add select menu to this column
123 // set the column value to true, and/or add "filter-select" class name to header
124 // '.first-name' : true,
125
126 // Exact match only
127 1 : function(e, n, f, i, $r, c, data) {
128 return e === f;
129 },
130
131 // Add these options to the select dropdown (regex example)
132 2 : {
133 "A - D" : function(e, n, f, i, $r, c, data) { return /^[A-D]/.test(e); },
134 "E - H" : function(e, n, f, i, $r, c, data) { return /^[E-H]/.test(e); },
135 "I - L" : function(e, n, f, i, $r, c, data) { return /^[I-L]/.test(e); },
136 "M - P" : function(e, n, f, i, $r, c, data) { return /^[M-P]/.test(e); },
137 "Q - T" : function(e, n, f, i, $r, c, data) { return /^[Q-T]/.test(e); },
138 "U - X" : function(e, n, f, i, $r, c, data) { return /^[U-X]/.test(e); },
139 "Y - Z" : function(e, n, f, i, $r, c, data) { return /^[Y-Z]/.test(e); }
140 },
141
142 // Add these options to the select dropdown (numerical comparison example)
143 // Note that only the normalized (n) value will contain numerical data
144 // If you use the exact text, you'll need to parse it (parseFloat or parseInt)
145 4 : {
146 "< $10" : function(e, n, f, i, $r, c, data) { return n < 10; },
147 "$10 - $100" : function(e, n, f, i, $r, c, data) { return n >= 10 && n <=100; },
148 "> $100" : function(e, n, f, i, $r, c, data) { return n > 100; }
149 }
150 }
151
152 }
153
154 });
155
156 });</script>
157
158 </head>
159 <body>
160 <div id="banner">
161 <h1>table<em>sorter</em></h1>
162 <h2>Custom Filter Widget Functions</h2>
163 <h3>Flexible client-side table sorting</h3>
164 <a href="index.html">Back to documentation</a>
165 </div>
166 <div id="main">
167
168 <p></p>
169 <br>
170 <div class="accordion">
171
172 <h3><a href="#">Notes</a></h3>
173 <div>
174 <ul>
175 <li>In <span class="version">v2.22.0</span>,
176 <ul>
177 <li>An additional <code>data</code> parameter was added to the filter functions. It is an object which contains all data provided to the filter type functions (see <a href="example-widget-filter-custom-search.html#how_to_add_custom_filter_types">all the data values here</a>).</li>
178 <li><span class="label warning">*WARNING*</span> In a future update, the filter function parameters will change to clean up the code (I know, it's messy)!
179 <pre class="prettyprint lang-js">filter_functions : {
180 // function(e, n, f, i, $r, c, data) {} <- current parameters
181 0 : function(c, data) {} // planned change (version undetermined)
182 }</pre>The <code>e</code> (exact table cell text), <code>n</code> (normalized table cell text), <code>f</code> (filter input value), <code>i</code> (column index) and <code>$r</code> (current row; jQuery object) are all already included in the <code>data</code> object.<p></p></li>
183 </ul>
184 </li>
185 <li>In <span class="version">v2.21.0</span>, the filter functions provide an additional parameter <code>c</code> which is the <code>table.config</code>.</li>
186 <li>In <span class="version updated">v2.17.0</span>, the <code>filter_functions</code> column can also be referenced by using a jQuery selector (e.g. class name or ID).</li>
187 <li>In <span class="version">v2.16.3</span>,
188 <ul>
189 <li>When a default filter select is added to a column, it is now parsed from the assigned parser, then sorted using the <code>textSorter</code> setting, and falls back to an alphanumeric sort.</li>
190 <li>Adding a class of <code>"filter-select-nosort"</code> will now leave the select options unsorted.</li>
191 </ul>
192 </li>
193 <li>In <span class="version">v2.14</span>, the <code>filter_saveFilters</code> option was added (default set to <code>false</code>); this demo has it set to <code>true</code> to provide an example.</li>
194 <li>In <span class="version">v2.11</span>, the filter functions provide an additional parameter <code>$r</code> providing a jQuery object of the current row being searched by the filter.</li>
195 <li>For <span class="version">v2.10.8</span>, the Age column includes the new <code>data-value</code> set to <code>&lt;30</code> which sets the default (starting) filter value (see <a href="index.html#widget-filter-defaultattrib">filter_defaultAttrib</a> for more details).</li>
196 <li>As of tablesorter <span class="version">v2.9</span>, this widget can no longer be applied to versions of tablesorter prior to version 2.8.</li>
197 <li>Custom filter widget option <code>filter_functions</code> was added in version 2.3.6.</li>
198 <li>jQuery v1.4.3+ required.</li>
199 </ul>
200 </div>
201
202 <h3><a href="#"><strong>Default Select</strong></a> <span class="xsmall">(&quot;First Name&quot; column)</span></h3>
203 <div>
204 <ul>
205 <li>To enable this type of select, set the <code>filter_functions</code> option for the column to <code>true</code>,<pre class="prettyprint lang-javascript">filter_functions : {
206
207 // Add select menu to this column
208 // set the column value to true, and/or add &quot;filter-select&quot; class name to header
209 '.first-name' : true
210
211 }</pre>or add a &quot;filter-select&quot; class to the column header cell (see code below).</li>
212 <li>The default option text, &quot;Select a name&quot;, is obtained from the header <code>data-placeholder</code> attribute of the column header cell. And when active, it will show all table rows.<pre class="prettyprint lang-html">&lt;th class=&quot;filter-select&quot; data-placeholder=&quot;Select a name&quot;&gt;First Name&lt;/th&gt;</pre></li>
213 <li>Add a &quot;filter-match&quot; class to only match instead of exactly match the selected value. Click on the &quot;Match&quot; button below to see the difference.<pre class="prettyprint lang-html">&lt;th class=&quot;filter-select filter-match&quot; data-placeholder=&quot;Select a name&quot;&gt;First Name&lt;/th&gt;</pre></li>
214 <li>The select is populated by the column text contents with repeated content combined (i.e. There are three &quot;Aaron&quot;'s in the first column, but only one in the dropdown.</li>
215 <li>Select options are automatically <del>alphanumerically</del> (new in v2.4) sorted. Changed in <span class="version updated">v2.16.3</span>:
216 <ul>
217 <li>The select contents are now parsed from the assigned parser, then sorted using the <a href="index.html#textsorter"><code>textSorter</code></a> option</li>
218 <li>Add a class name of <code>"filter-select-nosort"</code> to prevent sorting the select options (<span class="version">v2.16.3</span>).</li>
219 </ul>
220 </li>
221 <li>Please check out what the &quot;filter-onlyAvail&quot; class name does by reviewing the details below (in the &quot;Discount&quot; column) (<span class="version">v2.10.1</span>).</li>
222 </ul>
223 </div>
224
225 <h3><a href="#"><strong>Custom Filter Function</strong></a> <span class="xsmall">(&quot;Last Name&quot; column)</span></h3>
226 <div>
227 <ul>
228 <li>To enable this type of filter, add your custom function to the <code>filter_functions</code> option following this example:<pre class="prettyprint lang-javascript">filter_functions : {
229
230 // Exact match only
231 1 : function(e, n, f, i, $r, c, data) {
232 return e === f;
233 }
234
235 }</pre></li>
236 <li>The example shows you how to show only exact matches. The problem with this is that you can't see the matches while typing unless you set the <code>filter_searchDelay</code> option to be a bit longer.</li>
237 <li>Also, the example only checks for an exact match (<code>===</code>) meaning the <code>filter_ignoreCase</code> option is ignored, but other comparisons can be made using regex and the insensitive &quot;i&quot; flag.</li>
238 <li><span class="label warning">*NOTE*</span> If using an exact match function like this, consider setting the <a href="index.html#widget-filter-searchfiltered"><code>filter_searchFiltered</code></a> option to false. If it were set to <code>true</code>, the filter widget wouldn't know to search through the entire contents of the column if the content were only slightly different. To see this problem, search for <button type="button" data-filter-column="1">Evan</button> in the "Last Name" column, then add an "s" to the end to find "Evans". No results will show up, unless the search filtered option is <code>false</code>.</li>
239 <li>See the filter function information below for more details about the function parameters.</li>
240 </ul>
241 </div>
242
243 <h3><a href="#"><strong>Custom Select</strong></a> <span class="xsmall">(&quot;City&quot; or &quot;Total&quot; column)</span></h3>
244 <div>
245 <ul>
246 <li>To enable this type of select, add your custom options within the <code>filter_functions</code> option.</li>
247 <li>Each option is set as a &quot;key:value&quot; pair where the &quot;key&quot; is the actual text of the option and the &quot;value&quot; is the function associated with the option.</li>
248 <li>Here is an example using alphabetical comparisons (using regular expressions):<pre class="prettyprint lang-javascript">filter_functions : {
249
250 // Add these options to the select dropdown (regex example)
251 2 : {
252 &quot;A - D&quot; : function(e, n, f, i, $r, c, data) { return /^[A-D]/.test(e); },
253 &quot;E - H&quot; : function(e, n, f, i, $r, c, data) { return /^[E-H]/.test(e); },
254 &quot;I - L&quot; : function(e, n, f, i, $r, c, data) { return /^[I-L]/.test(e); },
255 &quot;M - P&quot; : function(e, n, f, i, $r, c, data) { return /^[M-P]/.test(e); },
256 &quot;Q - T&quot; : function(e, n, f, i, $r, c, data) { return /^[Q-T]/.test(e); },
257 &quot;U - X&quot; : function(e, n, f, i, $r, c, data) { return /^[U-X]/.test(e); },
258 &quot;Y - Z&quot; : function(e, n, f, i, $r, c, data) { return /^[Y-Z]/.test(e); }
259 }
260
261 }</pre></li>
262 <li>Here is an example using numerical comparisons:<pre class="prettyprint lang-javascript">filter_functions : {
263 // Add these options to the select dropdown (numerical comparison example)
264 // Note that only the normalized (n) value will contain numerical data
265 // If you use the exact text, you'll need to parse it (parseFloat or parseInt)
266 4 : {
267 &quot;&lt; $10&quot; : function(e, n, f, i, $r, c, data) { return n &lt; 10; },
268 &quot;$10 - $100&quot; : function(e, n, f, i, $r, c, data) { return n &gt;= 10 && n &lt;=100; },
269 &quot;&gt; $100&quot; : function(e, n, f, i, $r, c, data) { return n &gt; 100; }
270 }
271 }</pre></li>
272 <li>See the &quot;Filter function information&quot; section below for more details about the function parameters.</li>
273 </ul>
274 </div>
275
276 <h3><a href="#"><strong>Default Select with only available options</strong></a> <span class="xsmall">(&quot;Discount&quot; column)</span></h3>
277 <div>
278 <ul>
279 <li>This column uses the same method as the &quot;First Name&quot; column with one exception, it also includes the &quot;filter-onlyAvail&quot; class name in the header
280 cell:<pre class="prettyprint lang-html">&lt;th class=&quot;filter-select filter-onlyAvail&quot;&gt;Discount&lt;/th&gt;</pre></li>
281 <li>To see how this works, do the following:
282 <ul>
283 <li>First, filter the &quot;First Name&quot; column by selecting the name &quot;Clark&quot;</li>
284 <li>Now use the &quot;Discount&quot; filter select box, you'll notice that only the values associated with the first name of Clark are showing as options.</li>
285 </ul>
286 </li>
287 <li>Conversely, if you reset the filters, select &quot;44%&quot; in the &quot;Discount&quot; column, then look at the &quot;First Name&quot; filter selector, you'll notice that it still contains all of the original options; because the &quot;filter-onlyAvail&quot; class name is not included in that column's header cell.</li>
288 <li>Sorry, this functionality only works for default select filters.</li>
289 <li>This funcitonality was added in <span class="version">v2.10.1</span>.</li>
290 </ul>
291 </div>
292
293 <h3><a href="#"><strong>Filter function information</strong></a></h3>
294 <div>
295 <ul>
296 <li>The custom function must return a boolean value. If <code>true</code> is returned, the row will be shown if all other filters match; and if <code>false</code> is returned, the row will be hidden.
297 <pre class="prettyprint lang-javascript">function(e, n, f, i, $r, c, data) { return test; /* test should be a Boolean (true or false) */ }</pre>
298 </li>
299 <li>The <strong>exact text (e)</strong> of the table cell is a variable passed to the function. Note that numbers will need to be parsed to make comparisons.</li>
300 <li><strong>Normalized table cell data (n)</strong> is the next varibale passed to the function.
301 <ul>
302 <li>This data has been parsed by the assigned column parser, so make sure the same type of data is being compared as parsed data may not be what you expect.</li>
303 <li>Normalized numerical values within the table will be of numeric type and not of string type, as the sorter needs to use mathematical comparisons while sorting.</li>
304 <li>The data will be in lower-case if the <code>filter_ignoreCase</code> option is <code>true</code>.</li>
305 <li>Dates like in the last column of the table below will store the time in seconds since 1970 (using javascript's .getTime() function).</li>
306 <li>The percentage column will only store the number and not percentage sign.</li>
307 </ul>
308 </li>
309 <li>The <strong>filter input value (f)</strong> is the exact text entered by the user. If numerical, it will need to be parsed using parseFloat() or parseInt() to allow for making comparisons.</li>
310 <li>The <strong>column index (i)</strong> might be useful for obtaining more information from header, or something.</li>
311 <li>The <strong>row ($r)</strong> is the current table row (jQuery object) being searched (or filtered).
312 <ul>
313 <li>This allows access to any extra information within. To access the current cell, just use <code>$r.children().eq(i)</code>.</li>
314 <li><span class="label alert">* NOTE *</span> The row object provided is <em>not</em> attached to the table, so using something like <code>.parent()</code> or <code>.closest()</code> will not work!</li>
315 <li>For this reason, the next parameter (<code>c</code>) was added.</li>
316 </ul>
317 </li>
318 <li>The <strong>config (c)</strong> is the <code>table.config</code> (added <span class="version">v2.21.0</span>).</li>
319 <li>The <strong>data</strong> parameter is the same data passed to the filter types (see <a href="example-widget-filter-custom-search.html#how_to_add_custom_filter_types">all the data values here</a>; added <span class="version">v2.22.0</span>).</li>
320 </ul>
321 </div>
322 </div>
323
324 <h1>Demo</h1>
325 <button type="button" class="match" data-filter-column="0" data-filter-text="Denni">Toggle</button> <span id="mode" class="bright">false</span> &quot;filter-match&quot; class name on the "First Name" column<br>
326 <button type="button" class="search-type">Toggle</button> filter_searchFiltered : <span id="search-type" class="bright">true</span> (if true, the search is performed on already filtered rows, with some exceptions)<br>
327 <button type="button" class="reset">Reset Search</button>
328
329 <div id="demo"><table class="tablesorter">
330 <thead>
331 <tr>
332 <!-- add "filter-select" class or filter_functions : { '.first-name' : true } -->
333 <!-- add "filter-match" class to just match the content, so selecting "Denni" will also show "Dennis" -->
334 <th class="first-name filter-select" data-placeholder="Select a name">First Name</th>
335 <th data-placeholder="Exact matches only">Last Name</th>
336 <th data-placeholder="Choose a city">City</th>
337 <th data-value="<30">Age</th>
338 <th data-placeholder="Select a filter">Total</th>
339 <th class="filter-select filter-onlyAvail">Discount</th>
340 <th>Date</th>
341 </tr>
342 </thead>
343 <tbody>
344 <tr>
345 <td>Aaron</td>
346 <td>Johnson Sr</td>
347 <td>Atlanta</td>
348 <td>35</td>
349 <td>$5.95</td>
350 <td>22%</td>
351 <td>Jun 26, 2004 7:22 AM</td>
352 </tr>
353 <tr>
354 <td>Aaron</td>
355 <td>Johnson</td>
356 <td>Yuma</td>
357 <td>12</td>
358 <td>$2.99</td>
359 <td>5%</td>
360 <td>Aug 21, 2009 12:21 PM</td>
361 </tr>
362 <tr>
363 <td>Clark</td>
364 <td>Henry Jr</td>
365 <td>Tampa</td>
366 <td>51</td>
367 <td>$42.29</td>
368 <td>18%</td>
369 <td>Oct 13, 2000 1:15 PM</td>
370 </tr>
371 <tr>
372 <td>Denni</td>
373 <td>Henry</td>
374 <td>New York</td>
375 <td>28</td>
376 <td>$9.99</td>
377 <td>20%</td>
378 <td>Jul 6, 2006 8:14 AM</td>
379 </tr>
380 <tr>
381 <td>John</td>
382 <td>Hood</td>
383 <td>Boston</td>
384 <td>33</td>
385 <td>$19.99</td>
386 <td>25%</td>
387 <td>Dec 10, 2002 5:14 AM</td>
388 </tr>
389 <tr>
390 <td>Clark</td>
391 <td>Kent Sr</td>
392 <td>Los Angeles</td>
393 <td>18</td>
394 <td>$15.89</td>
395 <td>44%</td>
396 <td>Jan 12, 2003 11:14 AM</td>
397 </tr>
398 <tr>
399 <td>Peter</td>
400 <td>Kent Esq</td>
401 <td>Seattle</td>
402 <td>45</td>
403 <td>$153.19</td>
404 <td>44%</td>
405 <td>Jan 18, 2021 9:12 AM</td>
406 </tr>
407 <tr>
408 <td>Peter</td>
409 <td>Johns</td>
410 <td>Milwaukee</td>
411 <td>13</td>
412 <td>$5.29</td>
413 <td>4%</td>
414 <td>Jan 8, 2012 5:11 PM</td>
415 </tr>
416 <tr>
417 <td>Aaron</td>
418 <td>Evan</td>
419 <td>Chicago</td>
420 <td>24</td>
421 <td>$14.19</td>
422 <td>14%</td>
423 <td>Jan 14, 2004 11:23 AM</td>
424 </tr>
425 <tr>
426 <td>Bruce</td>
427 <td>Evans</td>
428 <td>Upland</td>
429 <td>22</td>
430 <td>$13.19</td>
431 <td>11%</td>
432 <td>Jan 18, 2007 9:12 AM</td>
433 </tr>
434 <tr>
435 <td>Clark</td>
436 <td>McMasters</td>
437 <td>Pheonix</td>
438 <td>18</td>
439 <td>$55.20</td>
440 <td>15%</td>
441 <td>Feb 12, 2010 7:23 PM</td>
442 </tr>
443 <tr>
444 <td>Dennis</td>
445 <td>Masters</td>
446 <td>Indianapolis</td>
447 <td>65</td>
448 <td>$123.00</td>
449 <td>32%</td>
450 <td>Jan 20, 2001 1:12 PM</td>
451 </tr>
452 <tr>
453 <td>John</td>
454 <td>Hood</td>
455 <td>Fort Worth</td>
456 <td>25</td>
457 <td>$22.09</td>
458 <td>17%</td>
459 <td>Jun 11, 2011 10:55 AM</td>
460 </tr>
461 </tbody>
462 </table></div>
463
464 <h1>Javascript</h1>
465 <div id="javascript">
466 <pre class="prettyprint lang-javascript"></pre>
467 </div>
468
469 <h1>HTML</h1>
470 <div id="html">
471 <pre class="prettyprint lang-html"></pre>
472 </div>
473
474 <div class="next-up">
475 <hr />
476 Next up: <a href="example-widget-filter-custom-search.html">Custom Filter Widget Search Types &rsaquo;&rsaquo;</a>
477 </div>
478
479 </div>
480
481 </body>
482 </html>