diff --git a/features/searchHighlight/dataTables.searchHighlight.js b/features/searchHighlight/dataTables.searchHighlight.js index c075994..4379afb 100644 --- a/features/searchHighlight/dataTables.searchHighlight.js +++ b/features/searchHighlight/dataTables.searchHighlight.js @@ -36,6 +36,17 @@ (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 $(document).on( 'init.dt.dth', function (e, settings, json) { var table = new $.fn.dataTable.Api( settings ); @@ -48,18 +59,17 @@ $(document).on( 'init.dt.dth', function (e, settings, json) { ) { table .on( 'draw.dt.dth column-visibility.dt.dth', function () { - // On each draw highlight search results, removing the old ones - body.unhighlight(); - - // Don't highlight the "not found" row - if ( table.rows( { filter: 'applied' } ).data().length ) { - body.highlight( table.search().split(' ') ); - } + highlight( body, table ); } ) .on( 'destroy', function () { // Remove event handler table.off( 'draw.dt.dth column-visibility.dt.dth' ); } ); + + // initial highlight for state saved conditions and initial states + if ( table.search() ) { + highlight( body, table ); + } } } );