jQuery plugin DataTables: How to highlight the current search text?

前端 未结 5 1077
青春惊慌失措
青春惊慌失措 2020-11-30 10:42

I have started using the DataTables plugin (v1.6.2) for jQuery(v1.4.2), and I would like to ask you if you know a settings

5条回答
  •  感动是毒
    2020-11-30 11:10

    This add on have better feature for highlighting search text. if you have created datatable in a dialog , then on dialog reopen you need to reinitialize datatable.

    In DatatableHighlighter.js

    jQuery.fn.dataTableExt.oApi.fnSearchHighlighting = function(oSettings) {
        // Initialize regex cache
        oSettings.oPreviousSearch.oSearchCaches = {};
    
        oSettings.oApi._fnCallbackReg( oSettings, 'aoRowCallback', function( nRow, aData, iDisplayIndex, iDisplayIndexFull) {
            // Initialize search string array
            var searchStrings = [];
            var oApi = this.oApi;
            var cache = oSettings.oPreviousSearch.oSearchCaches;
            // Global search string
            // If there is a global search string, add it to the search string array
            if (oSettings.oPreviousSearch.sSearch) {
                searchStrings.push(oSettings.oPreviousSearch.sSearch);
            }
            // Individual column search option object
            // If there are individual column search strings, add them to the search string array
    
         //   searchTxt=($('#filter_input input[type="text"]')?$('#filter_input input[type="text"]').val():"");
            var searchTxt = $('input[type="search"]').val();
            // console.log("txt" + searchTxt);
            if ((oSettings.aoPreSearchCols) && (oSettings.aoPreSearchCols.length > 0)) {
                for (var i in oSettings.aoPreSearchCols) {
                    if (oSettings.aoPreSearchCols[i].sSearch) {
                    searchStrings.push(searchTxt);
                    }
                }
            }
            // Create the regex built from one or more search string and cache as necessary
    
            if (searchStrings.length > 0) {
                var sSregex = searchStrings.join("|");
                if (!cache[sSregex]) {
                    var regRules = "("
                    ,   regRulesSplit = sSregex.split(' ');
    
                    regRules += "("+ sSregex +")";
                    for(var i=0; i)", 'ig');
                    //cache[sSregex] = new RegExp(regRules+"", 'ig');
                }
                var regex = cache[sSregex];
            }
    
            // Loop through the rows/fields for matches
            jQuery('td', nRow).each( function(i) {
    
                // Take into account that ColVis may be in use
                var j = oApi._fnVisibleToColumnIndex( oSettings,i);
    
                if (aData) {
                    // If there is a search string try to match
                    if ((typeof sSregex !== 'undefined') && (sSregex)) {
                        //For removing previous added 
                        var element = $(this);//convert string to JQuery element
                        element.find("span").each(function(index) {
                            var text = $(this).text();//get span content
                            $(this).replaceWith(text);//replace all span with just content
                        }).remove();
                        var newString = element.html();//get back new string
    
                        this.innerHTML = newString.replace( regex, function(matched) {
    
                            return ""+matched+"";
                        });
                    }
                    // Otherwise reset to a clean string
                    else {
                        //For removing previous added 
                        var element = $(this);//convert string to JQuery element
                        element.find("span").each(function(index) {
                            var text = $(this).text();//get span content
                            $(this).replaceWith(text);//replace all span with just content
                        }).remove();
                        var newString = element.html();
                        this.innerHTML = newString;//$(this).html()//$(this).text();
                    }
                }
            });
            return nRow;
        }, 'row-highlight');
        return this;
    };
    

    and call it like this ....

    $("#button").click(function() {
                      dTable = $('#infoTable').dataTable({"bPaginate": false,"bInfo" : false,"bFilter": true,"bSort":false, "autoWidth": false,"destroy": true,
                  "columnDefs": [
                                   { "width": "35%", "targets": 0 },
                                   { "width": "65%", "targets": 1 }
                                 ]});
              $(".dataTables_filter input[type='search']").val('');
              $("span[class='filterMatches']").contents().unwrap();
              dTable.fnSearchHighlighting();
              $("span[class='filterMatches']").contents().unwrap();
    
    
            $("#AboutDialog").dialog('open');
    
        });
    

提交回复
热议问题