Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

search exact match and highlight jquery datatable regex

In jquery datatable, I have to filter the result with exact match and highlight it. for exact match I am trying following code but it does not work. fiddle

table.aoPreSearchCols[ iCol ].sSearch = "^\s*"+'1'+"\s*$";
table.aoPreSearchCols[ iCol ].bRegex = false;
table.aoPreSearchCols[ iCol ].bSmart= false;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you need to use a word boundary, :

Matches a word boundary. A word boundary matches the position where a word character is not followed or preceeded by another word-character.

So when you have search term "limit" and the strings "my word has no limit", "it is unlimited" only the first string is a match. So

$('#search-inp').keyup(function(){
    var term = $(this).val(),
        regex = '\b' + term + '\b';

    table.columns(1).search(regex, true, false).draw();
})

Highlight

Define some static "highlight tags" to inject and remove in order to highlight search matches :

var hlBegin = '<strong class="highlight">',
    hlEnd = '</strong>';

Adding the highlight tags to column content :

function highlight(term) {
    var row, str,
        rowCount = table.rows().nodes().length,
        regexp = new RegExp('('+term+')', 'ig');

    for (row=0; row<rowCount; row++) {
        str = table.cell(row, 1).data();
        str = str.replace(regexp, function($1, match) { 
           return hlBegin + match + hlEnd;
        })
        table.cell(row, 1).data(str).draw();
    }        
}  

Removing highlight tags :

function removeHighlight() {
    var row, str,
        rowCount = table.rows().nodes().length;

    for (row=0; row<rowCount; row++) {
        str = table.cell(row, 1).data();
        str = str.replace(/(<([^>]+)>)/ig, '');
        table.cell(row, 1).data(str).draw();
    }        
}    

Setting it all together :

$('#search-inp').keyup(function(){
    var term = $(this).val(),
        regex = '\b' + term + '\b';

    removeHighlight();
    table.columns(1).search(regex, true, false);
    highlight(term);
})

forked fiddle -> http://jsfiddle.net/Lnvbnp6c/


Update. I got the impression (through comments) that whole words anywhere should be matched. If it is about matching a whole word in the beginning of the column :

regex = '^' + term + '\b';

http://jsfiddle.net/Lnvbnp6c/1/

If it is just about matching characters the column begins with, not nessecarily a whole word :

regex = '^' + term;

http://jsfiddle.net/Lnvbnp6c/2/

The last one is probably the one users would like the most, when they are typing in the search field.


As an alternative approach you could try to use a custom filter :

$('#search-inp').keyup(function() {
    var str,
        term = $(this).val(),
        regexp = new RegExp('\b' + term + '\b', 'ig');

    removeHighlight();    
    $.fn.dataTable.ext.search.push(
        function(settings, data, dataIndex ) {
            str = data[1];
            return regexp.test(str) ? true : false;
        }     
    );
    table.draw();
    highlight(term);    
    $.fn.dataTable.ext.search.pop();
})

demo with highlight as above -> http://jsfiddle.net/x96hzok4/

My impression is that this is a little bit faster. Certainly, if you have a lot of rows, and want to search on multiple columns, I think you should consider a custom filter, and consider not to make time-consuming complete regular expressions on all strings.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...