From 63021a4209a9cd90ef397e8a68b09822c59bc16f Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Thu, 22 Jan 2015 15:18:04 +0000 Subject: [PATCH] Search highlighting: Fix for state saving and an initial search term This fixes #124. Credit to @keeger for the suggested fix --- .../dataTables.searchHighlight.js | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) 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 ); + } } } );