Search highlighting: Fix for state saving and an initial search term

This fixes #124. Credit to @keeger for the suggested fix
pull/152/head
Allan Jardine 10 years ago
parent 36a239ad12
commit 63021a4209

@ -36,6 +36,17 @@
(function(window, document, $){ (function(window, document, $){
function highlight( body, table )
{
// Removing the old highlighting first
body.unhighlight();
// Don't highlight the "not found" row, so we get the rows using the api
if ( table.rows( { filter: 'applied' } ).data().length ) {
body.highlight( table.search().split(' ') );
}
}
// Listen for DataTables initialisations // Listen for DataTables initialisations
$(document).on( 'init.dt.dth', function (e, settings, json) { $(document).on( 'init.dt.dth', function (e, settings, json) {
var table = new $.fn.dataTable.Api( settings ); var table = new $.fn.dataTable.Api( settings );
@ -48,18 +59,17 @@ $(document).on( 'init.dt.dth', function (e, settings, json) {
) { ) {
table table
.on( 'draw.dt.dth column-visibility.dt.dth', function () { .on( 'draw.dt.dth column-visibility.dt.dth', function () {
// On each draw highlight search results, removing the old ones highlight( body, table );
body.unhighlight();
// Don't highlight the "not found" row
if ( table.rows( { filter: 'applied' } ).data().length ) {
body.highlight( table.search().split(' ') );
}
} ) } )
.on( 'destroy', function () { .on( 'destroy', function () {
// Remove event handler // Remove event handler
table.off( 'draw.dt.dth column-visibility.dt.dth' ); table.off( 'draw.dt.dth column-visibility.dt.dth' );
} ); } );
// initial highlight for state saved conditions and initial states
if ( table.search() ) {
highlight( body, table );
}
} }
} ); } );

Loading…
Cancel
Save