SearchFade
Hi everyone, I'm Colin and I joined the DataTables team late last year to head up testing and to provide customer support in the DataTables forum.
A recent question asked if DataTables, instead of only showing the matching filter results, could show both matching and unmatched records together with some colouring or shading on the rows to differentiate between the two. While not a feature that is available out-of-the-box with DataTables, by using its extensive customisability (callbacks and API methods) the SearchFade plug-in has been created.
As you can see in the example below, unmatched filter results are given a reduced opacity by being assigned the notMatched
CSS class. This could easily be changed to display a different font or a different background colour by modifying that class. SearchFade is fully compatible with all other DataTables features and customisations, as it only affects the rows currently being displayed.
Name | Position | Office | Salary |
---|---|---|---|
Tiger Nixon | System Architect | Edinburgh | $320,800 |
Garrett Winters | Accountant | Tokyo | $170,750 |
Ashton Cox | Junior Technical Author | San Francisco | $86,000 |
Cedric Kelly | Senior Javascript Developer | Edinburgh | $433,060 |
Airi Satou | Accountant | Tokyo | $162,700 |
Brielle Williamson | Integration Specialist | New York | $372,000 |
Herrod Chandler | Sales Assistant | San Francisco | $137,500 |
Rhona Davidson | Integration Specialist | Tokyo | $327,900 |
Colleen Hurst | Javascript Developer | San Francisco | $205,500 |
Sonya Frost | Software Engineer | Edinburgh | $103,600 |
Jena Gaines | Office Manager | London | $90,560 |
Quinn Flynn | Support Lead | Edinburgh | $342,000 |
Charde Marshall | Regional Director | San Francisco | $470,600 |
Haley Kennedy | Senior Marketing Designer | London | $313,500 |
Tatyana Fitzpatrick | Regional Director | London | $385,750 |
Michael Silva | Marketing Designer | London | $198,500 |
Paul Byrd | Chief Financial Officer (CFO) | New York | $725,000 |
Gloria Little | Systems Administrator | New York | $237,500 |
Bradley Greer | Software Engineer | London | $132,000 |
Dai Rios | Personnel Lead | Edinburgh | $217,500 |
Jenette Caldwell | Development Lead | New York | $345,000 |
Yuri Berry | Chief Marketing Officer (CMO) | New York | $675,000 |
Caesar Vance | Pre-Sales Support | New York | $106,450 |
Doris Wilder | Sales Assistant | Sydney | $85,600 |
Angelica Ramos | Chief Executive Officer (CEO) | London | $1,200,000 |
Gavin Joyce | Developer | Edinburgh | $92,575 |
Jennifer Chang | Regional Director | Singapore | $357,650 |
Brenden Wagner | Software Engineer | San Francisco | $206,850 |
Fiona Green | Chief Operating Officer (COO) | San Francisco | $850,000 |
Shou Itou | Regional Marketing | Tokyo | $163,000 |
Michelle House | Integration Specialist | Sydney | $95,400 |
Suki Burks | Developer | London | $114,500 |
Prescott Bartlett | Technical Author | London | $145,000 |
Gavin Cortez | Team Leader | San Francisco | $235,500 |
Martena Mccray | Post-Sales support | Edinburgh | $324,050 |
Unity Butler | Marketing Designer | San Francisco | $85,675 |
Howard Hatfield | Office Manager | San Francisco | $164,500 |
Hope Fuentes | Secretary | San Francisco | $109,850 |
Vivian Harrell | Financial Controller | San Francisco | $452,500 |
Timothy Mooney | Office Manager | London | $136,200 |
Jackson Bradshaw | Director | New York | $645,750 |
Olivia Liang | Support Engineer | Singapore | $234,500 |
Bruno Nash | Software Engineer | London | $163,500 |
Sakura Yamamoto | Support Engineer | Tokyo | $139,575 |
Thor Walton | Developer | New York | $98,540 |
Finn Camacho | Support Engineer | San Francisco | $87,500 |
Serge Baldwin | Data Coordinator | Singapore | $138,575 |
Zenaida Frank | Software Engineer | New York | $125,250 |
Zorita Serrano | Software Engineer | San Francisco | $115,000 |
Jennifer Acosta | Junior Javascript Developer | Edinburgh | $75,650 |
Cara Stevens | Sales Assistant | New York | $145,600 |
Hermione Butler | Regional Director | London | $356,250 |
Lael Greer | Systems Administrator | London | $103,500 |
Jonas Alexander | Developer | San Francisco | $86,500 |
Shad Decker | Regional Director | Edinburgh | $183,000 |
Michael Bruce | Javascript Developer | Singapore | $183,000 |
Donna Snider | Customer Support | New York | $112,000 |
Usage
Using SearchFade with your tables is easily done. First, include the Javascript and the CSS for the plug-in on your page:
To enable and use the SearchFade input, make use of layout
to position the control around the table - e.g. this will replace the built in default search for DataTables:
$('#myTable').DataTable({
layout: {
topEnd: 'searchFade'
}
});
If you are using a legacy 1.x version of DataTables, you can position it using the dom
option with F
for SearchFade!:
$('#myTable').DataTable({
dom: 'Flfrtip'
});
Or finally, you can declare it at the table initialisation stage, then place the search element's node wherever you choose (which is currently the easier option if you are using a styling integration such as Bootstrap or Semantic UI):
var table = $('#myTable').DataTable({
searchFade: true
});
table.searchFade().node().appendTo('body');
Implementation
SearchFade was implemented with a different input element to enter the search term, since the standard search input removes unmatched records and is not configurable. In the example initialisations above, the standard search would still be available, but could be removed by taking the f
option out of the dom
string.
The code below uses the API to implement our SearchFade search. Since the row highlighting is only concerned with visible rows, whenever a key is pressed in the input element or if the table is redrawn (perhaps as a result of a page change), all visible rows are checked for the search term, and if not matched, a different CSS styling (by default, this is reduced opacity) is assigned.
var table = new $.fn.dataTable.Api(settings);
var searchFade = $('<div class="searchFade"/>');
table.settings()[0].searchFadeNode = searchFade;
_draw(table, searchFade);
// Trigger a search
searchFade.on('keyup redraw', 'input', function() {
table.rows(':visible').every(function(rowIdx, tableLoop, rowLoop) {
var present = true;
if ($('.searchFadeInput' + table.settings()[0].sTableId).val().length) {
present = table
.row(rowIdx)
.data()
.some(function(v) {
return v.match(new RegExp($('.searchFadeInput' + table.settings()[0].sTableId).val(), 'i')) != null;
});
}
$(table.row(rowIdx).node()).toggleClass('notMatched', !present);
});
});
table.on('draw', function() {
$('.searchFadeInput').trigger('redraw');
});
// API method to get the searchFade container node
this.node = function() {
return searchFade;
};
We hope you enjoy this new plugin and have an opportunity to use it on your tables. If you have questions or comments, please leave them in the DataTables forum.
Happy coding!!!