Ammar Khan
Ammar Khan

Reputation: 71

How to show "No Record Found" meassage in Table filter

I'm applying filter on my table on the basis of two tds. Filter is working but I want to show a No Recod Found message, if there is no value match. Here is a sample demo:

$("#filter").click(function () {
  var tdScoring,tdEarning;
  var scoring=$("#scoring1").val();
  var earning = $("#earning1").val();
  table = document.getElementById("mastermindTable");
  tr = table.getElementsByTagName("tr");

  for (i = 0; i < tr.length; i++) {
   tdScoring = tr[i].getElementsByTagName("td")[4];
   tdEarning = tr[i].getElementsByTagName("td")[3];

   if (tdScoring || tdEarning) {
    if (tdScoring.innerHTML.indexOf(scoring) == 0 && tdEarning.innerHTML.indexOf(earning) == 0) {
      tr[i].style.display = ""; 
    }
    else {
      tr[i].style.display = "none";
    }
  }

}

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-sm-6 col-md-3 p0">
              <div class="fixed-table-toolbar">
                <div class="bars pull-left">
                  <div id="toolbar">
                    <select class="form-control input_billing" id="earning1">
                      <option value="">Earning</option>
                        <option value="100">100</option>                                               <option value="150">150</option>                                               <option value="250">250</option>

                    </select>
                  </div>
                </div>
                <div class="bars pull-left">
                  <div id="toolbar">
                    <select class="form-control input_billing" id="scoring1">
                      <option value="">Scoring</option>
                        <option value="70">70</option>                                               <option value="90">90</option>
                        <option value="100">100</option>
                    </select>
                  </div>
                </div>

                <button id="filter" class="btn btn-success" style="margin-top:8px;" type="button">Filter</button>
              </div>
            </div>
<table class="table table-condensed" id="mastermindTable">
  <thead>
                  <tr>
                    <th width="18%" align="left">Name</th>
                    <th width="26%" align="left">Email </th>
                    <th width="20%" align="left">Expertise</th>
                    <th width="16%" align="left">Earning</th>
                    <th width="16%" align="left">Scoring</th>
                    <th width="4%" align="left">Status</th>
                  </tr>
                </thead>
                <tbody id="mastermindTableBody">
                    <tr> 
                      <td>Ammar</td>
                      <td>[email protected]</td>
                      <td>PHP</td>
                      <td>100</td>
                      <td>70</td>
                     <td>Active</td>                    
                  </tr>
                  <tr> 
                      <td>Arsnel</td>
                      <td>[email protected]</td>
                      <td>Larave</td>
                      <td>150</td>
                      <td>90</td>
                     <td>Active</td>                    
                  </tr>
                  <tr> 
                      <td>Abeera</td>
                      <td>[email protected]</td>
                      <td>CI</td>
                      <td>250</td>
                      <td>110</td>
                     <td>InActive</td>                    
                  </tr>
              </tbody>
            </table>

You can also mention another simple way to filter a table and achieve this. I only want to acieve this with jQuery and JS no plugin please.

Upvotes: 4

Views: 16096

Answers (3)

Mobeen Sarwar
Mobeen Sarwar

Reputation: 524

just add a new tr with a message of No Record Found and with display:none. jsfiddle is here:

demo

    <tr id="noRecordTR" style="display:none"> 
                  <td>No Record Found</td>                              
    </tr>

JavaScript

 if(hidden_rows==rows){
 for (i = 2; i < tr.length; i++) {
  tr[i].style.display = "none";
  }
  document.getElementById('noRecordTR').style.display = "";
 }

Upvotes: 6

daremachine
daremachine

Reputation: 2788

Its simple. Just add this code under your FOR function.

// end your for     
$('#mastermindTable tbody tr[data-no-results-found]').remove();

if($('#mastermindTable tbody tr:visible').length === 0) {
    $('#mastermindTable tbody').append('<tr data-no-results-found><td colspan="6">NO RESULTS FOUND</td></tr>');
}

JSFIDDLE from your original post


UPDATE

Or you can write/use universal function any table which handle "no rows found" message.

function

$.fn.AddNoRowsFound = function() {
    if($(this).find('tbody tr:not([data-no-results-found]):visible').length > 0) {
    $(this).find('tbody tr[data-no-results-found]').hide();
  }
  else {
    $(this).find('tbody tr[data-no-results-found]').show();
  }
};

You need call function with table on init/page load. Then if you have any actions/filters listeners just add call into the listener.

$(table_selector).AddNoRowsFound();

And also you need add row with message into table > body.

<tr data-no-results-found style="display:none"><td colspan="6">NO RESULTS FOUND</td></tr>

This is universal thing for any table.

JSFIDDLE2

Upvotes: 3

Arthur Marubayashi
Arthur Marubayashi

Reputation: 34

Have you tried to draw an simple div after your table with content "No Recod Found message" and display none? If you have results -> add the display none style, else -> remove the display none style.

Upvotes: 0

Related Questions