jQuery: Remove table row

0

I'm trying to remove rows from the table where, in the Employee column, the name does not contain the string "o", as I did in the code below. The problem is that even returning false for comparison if "o" is contained in "Joseph", the line is not deleted.

HTML:

<!DOCTYPEhtml><html><head><metacharset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script src="https://code.jquery.com/jquery-3.1.0.js"></script></head><body><table><thead><tr><th>Código</th><th>Funcionário</th><th>Cargo</th></tr></thead><tbody><tr><td>1</td><td>João</td><td>AnalistadeSistemas</td></tr><tr><td>2</td><td>Maria</td><td>Advogada</td></tr><tr><td>3</td><td>José</td><td>EngenheiroCivil</td></tr><tr><td>4</td><td>Joãozinho</td><td>Desenvolvedor</td></tr></tbody></table></body></html>

JS:

(function(){$(document).ready(function(){$('tabletd:nth-child(2)').each(function(indice){vartxtDigitado="Oã";
            var pattern = new RegExp(txtDigitado, 'i');

            var item_celula = $(this).text();

            if(pattern.test(item_celula)) {
              $('table tbody tr').eq(indice+1).remove();
            }
        });
    });
})();
    
asked by anonymous 14.12.2016 / 23:31

1 answer

3

My friend, I had to modify his function a bit, but I think it worked. I took the table row that we want to delete first, looped, and applied the test. When it returns 'false' the line is deleted:

(function(){
    $(document).ready(function(){
        //pega a 'tr' no corpo da tabela
        $('table tbody tr').each(function(indice){
            var txtDigitado = "Oã";
            var pattern = new RegExp(txtDigitado, 'i');

            //pega o texto na 2a. 'td'
            var item_celula = $(this).find('td:eq(1)').text();

            if(pattern.test(item_celula) == false) {
                //caso passe no teste, remove a linha atual
                $(this).remove();
            }
        });
    });
})();

I hope I have helped.

    
15.12.2016 / 16:49