Search - Show Message if results are not found

Asked

Viewed 897 times

1

I’m having a problem with javascript, I took a js that does the search in the table, but when there are no records it just shows nothing, how do I put a "Return" with some sentence when there are no records,I tried a few times but without success. Follows:

// Write on keyup event of keyword input element
$("#search").keyup(function(){
    _this = this;
    // Show only matching TR, hide rest of them
    $.each($("#table tbody").find("tr"), function() {
        console.log($(this).text());
        if($(this).text().toLowerCase().indexOf($(_this).val().toLowerCase()) == -1)
            $(this).hide();
        else
            $(this).show();
    });
});
  • 1

    Where do you want to show this phrase? Instead of the table?

  • That, set the tfoot tag there in html with an id, and now when showing no result (which was not found), give an append there in tfoot

  • Take a look at my answer. =)

3 answers

1

I added ID in tfoot and created a flag that receives the amount of records from the table and subtracted every time you hide an item, if at the end the flag value is 0 I show the message.

var table = $("#table tbody").find("tr");

$("#search").keyup(function() {
  _this = this;
  flag = table.length;

  $.each(table, function() {
    if ($(this).text().toLowerCase().indexOf($(_this).val().toLowerCase()) == -1) {
      $(this).hide();
      flag--;
    } else {
      $(this).show();
    }
  });

  if (!(flag > 0))
    $("#retorno").html("Não foram encontrados resultados");
  else
    $("#retorno").html("");
});

Functional example: https://jsfiddle.net/scs5jo2e/

  • Any resemblance to my reply and mere coincidence, detail tests there that it is not working right with 1 record found.

  • Yes, I used your fiddle as a base and thank you for the warning, corrected! =)

  • Thank you so much for your time.

1

All you’ll need to do is identify how many tr are with display:none and when they are showing the message stating that there is no data for the given filter.

Example:

JS:

var table = $("#table tbody").find("tr");
var mensagem = $("#mensagem");
mensagem.hide();
$("#search").keyup(function() {
  _this = this;
  $.each(table, function() {
    if ($(this).text().toLowerCase().indexOf($(_this).val().toLowerCase()) == -1)
      $(this).hide();
    else
      $(this).show();
  });

  var tableDisplay = table.css('display');

  mensagem.hide();
  if (tableDisplay === 'none') {
    mensagem.show();
  }

});

HTML:

<form class="navbar-form navbar-left" role="search">
  <div class="form-group">
    <input type="text" class="form-control" placeholder="Search" id="search">
  </div>
</form>

<table class="table table-hover" id="table">
  <tr>
    <td>Gabriel</td>
    <td>Rodrigues</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Highlander</td>
    <td>Imortal</td>
    <td>94</td>
  </tr>
</table>
<div id="mensagem">Sem resultados</div>

Outside the each() I put a variable to show the display of tr, when all are hidden it will enter the condition that will show the message.

See working on jsfiddle

  • Thanks for the willingness to help, I was hoping to trade Alert for an append directly on tfoot, something similar to this: var table = $("#tbody"). find("tr"); $("#search"). keyup(Function() { _this = this; $.each(table, Function() { if ($(this). text(). toLowerCase(). indexof($(_this). val(). toLowerCase()) == -1) $(this). Hide(); Else $(this). show(); }); var tableDisplay = table.css('display'); if (tableDisplay === 'None') { $("#paragraphAdictionaries"). append("<tr>" + "<td colspan="3">No results</td>" + "</tr>"); } });

  • Instead of working with tfoot that will require css because it will get badly formatted with td put a div at the end and leave it with the message, I will edit with example

  • Now yes it worked, thank you very much for the rss help

  • @Rafaelribeiro If my answer solved your problem mark it as accepted ;)

  • This one worked "correctly", sorry for the delay but in this javascript until when it finds a record it tends to present that there were no results found.

0

See if this works:

<div id="msg"></div>

.....

$("#search").keyup(function(){
    _this = this;
    // Show only matching TR, hide rest of them
    $.each($("#table tbody").find("tr"), function() {
        console.log($(this).text());
        if($(this).text().toLowerCase().indexOf($(_this).val().toLowerCase()) == -1)
            $(this).hide();
            $("#msg").html("<span class="alert-warning">Ops, nada foi encontrado</span>");
        else
            $(this).show();
            $("#msg").html("<span class="alert-success">Opa, encontramos alguma coisa</span>");
    });
});
  • That’s exactly what I want to do but instead of adding in the div put in tfoot, this code didn’t work.

  • And so instead of $("#msg"). html("<span class="Alert-Warning">Ops, nothing found</span>"); Vc use this: $("#table tfoot"). find("td"). html("blablabla"); Or still: <tfoot> <tr> <td><span id="msg"></span></td>&/td> </tr> </tfoot> Ai using yesterday’s response tip

Browser other questions tagged

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