| 9 |
lars |
1 |
/*! SearchHighlight for DataTables v1.0.1
|
|
|
2 |
* 2014 SpryMedia Ltd - datatables.net/license
|
|
|
3 |
*/
|
|
|
4 |
|
|
|
5 |
/**
|
|
|
6 |
* @summary SearchHighlight
|
|
|
7 |
* @description Search term highlighter for DataTables
|
|
|
8 |
* @version 1.0.1
|
|
|
9 |
* @file dataTables.searchHighlight.js
|
|
|
10 |
* @author SpryMedia Ltd (www.sprymedia.co.uk)
|
|
|
11 |
* @contact www.sprymedia.co.uk/contact
|
|
|
12 |
* @copyright Copyright 2014 SpryMedia Ltd.
|
|
|
13 |
*
|
|
|
14 |
* License MIT - http://datatables.net/license/mit
|
|
|
15 |
*
|
|
|
16 |
* This feature plug-in for DataTables will highlight search terms in the
|
|
|
17 |
* DataTable as they are entered into the main search input element, or via the
|
|
|
18 |
* `search()` API method.
|
|
|
19 |
*
|
|
|
20 |
* It depends upon the jQuery Highlight plug-in by Bartek Szopka:
|
|
|
21 |
* http://bartaz.github.io/sandbox.js/jquery.highlight.js
|
|
|
22 |
*
|
|
|
23 |
* Search highlighting in DataTables can be enabled by:
|
|
|
24 |
*
|
|
|
25 |
* * Adding the class `searchHighlight` to the HTML table
|
|
|
26 |
* * Setting the `searchHighlight` parameter in the DataTables initialisation to
|
|
|
27 |
* be true
|
|
|
28 |
* * Setting the `searchHighlight` parameter to be true in the DataTables
|
|
|
29 |
* defaults (thus causing all tables to have this feature) - i.e.
|
|
|
30 |
* `$.fn.dataTable.defaults.searchHighlight = true`.
|
|
|
31 |
*
|
|
|
32 |
* For more detailed information please see:
|
|
|
33 |
* http://datatables.net/blog/2014-10-22
|
|
|
34 |
*/
|
|
|
35 |
|
|
|
36 |
(function(window, document, $){
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
function highlight( body, table )
|
|
|
40 |
{
|
|
|
41 |
// Removing the old highlighting first
|
|
|
42 |
body.unhighlight();
|
|
|
43 |
|
|
|
44 |
// Don't highlight the "not found" row, so we get the rows using the api
|
|
|
45 |
if ( table.rows( { filter: 'applied' } ).data().length ) {
|
|
|
46 |
body.highlight( $.trim( table.search() ).split(/\s+/) );
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
// Listen for DataTables initialisations
|
|
|
51 |
$(document).on( 'init.dt.dth', function (e, settings, json) {
|
|
|
52 |
var table = new $.fn.dataTable.Api( settings );
|
|
|
53 |
var body = $( table.table().body() );
|
|
|
54 |
|
|
|
55 |
if (
|
|
|
56 |
$( table.table().node() ).hasClass( 'searchHighlight' ) || // table has class
|
|
|
57 |
settings.oInit.searchHighlight || // option specified
|
|
|
58 |
$.fn.dataTable.defaults.searchHighlight // default set
|
|
|
59 |
) {
|
|
|
60 |
table
|
|
|
61 |
.on( 'draw.dt.dth column-visibility.dt.dth column-reorder.dt.dth', function () {
|
|
|
62 |
highlight( body, table );
|
|
|
63 |
} )
|
|
|
64 |
.on( 'destroy', function () {
|
|
|
65 |
// Remove event handler
|
|
|
66 |
table.off( 'draw.dt.dth column-visibility.dt.dth column-reorder.dt.dth' );
|
|
|
67 |
} );
|
|
|
68 |
|
|
|
69 |
// initial highlight for state saved conditions and initial states
|
|
|
70 |
if ( table.search() ) {
|
|
|
71 |
highlight( body, table );
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
} );
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
})(window, document, jQuery);
|