Tuesday 8th May, 2018
By Colin Marks

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.

NamePositionOfficeSalary
Tiger NixonSystem ArchitectEdinburgh$320,800
Garrett WintersAccountantTokyo$170,750
Ashton CoxJunior Technical AuthorSan Francisco$86,000
Cedric KellySenior Javascript DeveloperEdinburgh$433,060
Airi SatouAccountantTokyo$162,700
Brielle WilliamsonIntegration SpecialistNew York$372,000
Herrod ChandlerSales AssistantSan Francisco$137,500
Rhona DavidsonIntegration SpecialistTokyo$327,900
Colleen HurstJavascript DeveloperSan Francisco$205,500
Sonya FrostSoftware EngineerEdinburgh$103,600
Jena GainesOffice ManagerLondon$90,560
Quinn FlynnSupport LeadEdinburgh$342,000
Charde MarshallRegional DirectorSan Francisco$470,600
Haley KennedySenior Marketing DesignerLondon$313,500
Tatyana FitzpatrickRegional DirectorLondon$385,750
Michael SilvaMarketing DesignerLondon$198,500
Paul ByrdChief Financial Officer (CFO)New York$725,000
Gloria LittleSystems AdministratorNew York$237,500
Bradley GreerSoftware EngineerLondon$132,000
Dai RiosPersonnel LeadEdinburgh$217,500
Jenette CaldwellDevelopment LeadNew York$345,000
Yuri BerryChief Marketing Officer (CMO)New York$675,000
Caesar VancePre-Sales SupportNew York$106,450
Doris WilderSales AssistantSidney$85,600
Angelica RamosChief Executive Officer (CEO)London$1,200,000
Gavin JoyceDeveloperEdinburgh$92,575
Jennifer ChangRegional DirectorSingapore$357,650
Brenden WagnerSoftware EngineerSan Francisco$206,850
Fiona GreenChief Operating Officer (COO)San Francisco$850,000
Shou ItouRegional MarketingTokyo$163,000
Michelle HouseIntegration SpecialistSidney$95,400
Suki BurksDeveloperLondon$114,500
Prescott BartlettTechnical AuthorLondon$145,000
Gavin CortezTeam LeaderSan Francisco$235,500
Martena MccrayPost-Sales supportEdinburgh$324,050
Unity ButlerMarketing DesignerSan Francisco$85,675
Howard HatfieldOffice ManagerSan Francisco$164,500
Hope FuentesSecretarySan Francisco$109,850
Vivian HarrellFinancial ControllerSan Francisco$452,500
Timothy MooneyOffice ManagerLondon$136,200
Jackson BradshawDirectorNew York$645,750
Olivia LiangSupport EngineerSingapore$234,500
Bruno NashSoftware EngineerLondon$163,500
Sakura YamamotoSupport EngineerTokyo$139,575
Thor WaltonDeveloperNew York$98,540
Finn CamachoSupport EngineerSan Francisco$87,500
Serge BaldwinData CoordinatorSingapore$138,575
Zenaida FrankSoftware EngineerNew York$125,250
Zorita SerranoSoftware EngineerSan Francisco$115,000
Jennifer AcostaJunior Javascript DeveloperEdinburgh$75,650
Cara StevensSales AssistantNew York$145,600
Hermione ButlerRegional DirectorLondon$356,250
Lael GreerSystems AdministratorLondon$103,500
Jonas AlexanderDeveloperSan Francisco$86,500
Shad DeckerRegional DirectorEdinburgh$183,000
Michael BruceJavascript DeveloperSingapore$183,000
Donna SniderCustomer SupportNew 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:

CSS
JS

You can either enabled it as a dom option (F for SearchFade!):

$('#myTable').DataTable({
    dom: 'Flfrtip'
});

Or, 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!!!