开发者

Removing rows that do not contain search-term with jQuery

开发者 https://www.devze.com 2023-04-11 09:15 出处:网络
I\'m using the following JQuery to filter rows on a datatable, which works fine... yuiDtFilter = function(tableDivId, filter) {

I'm using the following JQuery to filter rows on a datatable, which works fine...

yuiDtFilter = function(tableDivId, filter) {
     //custom jQuery function defines case-insensitive fn:Contains, use default fn:contains for case-sensitive search
     jQuery.expr[':'].Contains = function(a,i,m){
       return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
     };
     $("#" + tableDivId + " .yui-d开发者_高级运维t-data").find('tr').hide();
     $("#" + tableDivId + " .yui-dt-data").find('td:Contains("' + filter + '")').parents('tr').show();
 }

However I have a need for the filter work in the opposite way. I need it to remove rows that don't match the search terms.

I've found out that I need to use 'not()', but I've spent most of the day in vain trying to get it to work (using every example I can find).

I've tried many variations of -

$("#" + tableDivId + " .yui-dt-data")
    .find(:not(('td:Contains("' + filter + '")'))
    .parents('tr').remove();

Could anyone give me a hand using my code as a starting point?


Try

$("#" + tableDivId + " .yui-dt-data").find('td').not(':contains("' + filter + '")').parents('tr').remove();

or

$("#" + tableDivId + " .yui-dt-data").find( 'td:not(:contains("' + filter + '"))' ).parents('tr').remove()


Remove row from HTML table that doesn't contains specific text or string using jquery.

Note: If there are only two column in HTML table, we can use "last-child" attribute to find.

*$(document).ready(function(){
    $("#tabledata tbody .mainTR").each(function(){
        var lastTD = $(this).find("td:last-child");
        var lastTdText = lastTD.text().trim();
        if(!lastTdText.includes("DrivePilot")){
            $(this).remove();
        }
    });
  });

Note: If there are more than two column in HTML table, we can use "nth-child(2)" attribute to find.

Passing column index with "nth-child(column index)"

$(document).ready(function(){
    $("#tabledata tbody .mainTR").each(function(){
        var lastTD = $(this).find("td:nth-child(2)");
        var lastTdText = lastTD.text().trim();
        if(!lastTdText.includes("DrivePilot")){
            $(this).remove();
        }
    });
  });

Note: "DrivePilot" is nothing but text or string

0

精彩评论

暂无评论...
验证码 换一张
取 消