Search in jQuery displaying the nearest TR as header!

Asked

Viewed 312 times

0

I GOT THE ANSWER ON THE AMERICAN STACK: https://stackoverflow.com/a/46654887/588842

I got the following HTML and JS:

FOLLOW EXTERNAL FIDDLE LINK: https://jsfiddle.net/qpouvped/

$(".filter-nome").keyup(function(){

  var valor = $(this).val();
  $(".lista-certidoes tbody tr").each(function(index){

    $row = $(this);
    var id = $row.find("td:nth-child(2)").text();
    if (id.indexOf(valor) != 0) {
      $(this).hide();
    }
    else {
      $(this).show();
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
<div class="row">
  <div class="col-12 col-lg-4">
<label for="nome">Nome</label>
<input type="text" class="form-control filter-nome" value="">
  </div>
  <div class="col-12 col-lg-4">
<label for="oab">Nº</label>
<input type="text" class="form-control filter-oab" value="">
  </div>
  <div class="col-12 col-lg-4">
<label for="protocolo">Nº Protocolo</label>
<input type="text" class="form-control filter-protocolo" value="">
  </div>
</div>
<table class="table table-stripped table-bordered lista-certidoes">
    	<thead>
    		<tr>
    			<th>Nº</th>
    			<th>Nome</th>
    			<th>Nº PROTOCOLO</th>
    		</tr>
    	</thead>
    	<tbody>
    		<tr>
    			<td colspan="3" class="cab_interno">A</td>
    		</tr>
    		<tr>
    			<td >137418</td>
    			<td >Nonono Nonono Nonono</td>
    			<td >11225566</td>
    		</tr>
    		<tr>
    			<td colspan="3" class="cab_interno">B</td>
    		</tr>
    		<tr>
    			<td >122222</td>
    			<td >Nonono Nonono Nonono</td>
    			<td >11225566</td>
    		</tr>
    		...
    	</tbody>
    </table>

So far so good, working normal, however, I would like to leave displaying the TR that has the td with the class .cab_interno, which is the Letter corresponding to that "part" of the list.

I tried to use this below, along with the $(this).show();, but it didn’t work..

$(this).closest('tr > td.cab_interno').fadeIn('slow', function() {
    console.log("OK");
});

Somebody give me a hand there ?

  • In your html I could not find where the class "filter-name" could edit the question?

  • Put, it is a simple input, I also put a link with the external Fiddle

1 answer

0

I separated the table into several <tbody> to group the data.

After the search on input will be displayed only the tbody that has in some of its lines a name equal to the value typed in input.

It is worth remembering that the Javascript case differentiation from minuscule.

$(".filter-nome").keyup(function(){

  var valor = $(this).val();
  $(".cab_interno tr").each(function(index){
    
    //Tbody atual:
    $cabecalho  = $(this).parent("tbody"); 
    
    //linha do tbody:
    $linha      = $(this); 
    
    //Coluna nome:
    $nome       = $linha.find("td:nth-child(2)").text(); 

    if ($nome.indexOf(valor) != 0) {
      //$cabecalho.hide();
      $linha.hide();
      
    }
    else {
      $linha.show();
      $cabecalho.find("tr:first-child").show();
    }
  });
});
.cab_interno th{
  background-color: #333;
  color: #fff;
  text-align: center;
  padding: 8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
<div class="row">
  <div class="col-12 col-lg-4">
<label for="nome">Nome</label>
<input type="text" class="form-control filter-nome" value="">
  </div>
<table class="table table-stripped table-bordered lista-certidoes">
    	<thead>
    		<tr>
    			<th>Nº</th>
    			<th>Nome</th>
    			<th>Nº PROTOCOLO</th>
    		</tr>
    	</thead>
    	<tbody class="cab_interno">
    		<tr>
    			<th colspan="3">A</th>
    		</tr>
    		<tr>
    			<td>137418</td>
    			<td>Texto A</td>
    			<td>11225566</td>
    		</tr>
      </tbody>
      <tbody class="cab_interno">
    		<tr>
    			<th colspan="3">B</th>
    		</tr>
    		<tr>
    			<td>122222</td>
    			<td>Texto 1B</td>
    			<td>11225566</td>
    		</tr>
        <tr>
    			<td>122222</td>
    			<td>Texto 2B</td>
    			<td>11225566</td>
    		</tr>
    	</tbody>
      <tbody class="cab_interno">
         <tr>
            <th colspan="3">C</th>
        </tr>
        <tr>
            <td>122222</td>
            <td>Nononono</td>
            <td>11225566</td>
        </tr>
         <tr>
            <td>133</td>
            <td>Nanana Nonono Nonono</td>
            <td>11225566</td>
        </tr>
      </tbody>
    </table>

  • It worked not.. if I look for Text B, he finds nothing... I want to search by person’s name, and also display the corresponding Alphabet Letter. The way you did, it only shows off if I get the letter B from "head"

  • Dude, look at this Fiddle, I copied his code, and it’s not working. Where am I wrong? Yours worked just fine... https://jsfiddle.net/qpouvped/1/

  • https://jsfiddle.net/qpouvped/1/

  • I get it, it’s a big list, with several names inside each letter... but vlws help it.. I’ll try something here...

  • I changed the code by grouping it by <tbody> and searching all its lines, that way when one of the lines contains the name typed only that tbody will be displayed. See if the result is what you want.

  • Apparently it worked, but I needed it to be displayed only to the person sought, and not all <tbody>. I asked for help on Stackoverflow in English, they put an answer there! I’ll leave it here in the post! Thanks a lot for the help and time Caique..

  • Leave it there, it’s another way of showing...

  • Now I think it was, I tested it with several lines inside and it was ok

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.