Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 468
Ammar Khan Avatar asked Mar 06 '23 09:03

Ammar Khan


1 Answers

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 = "";
 }
like image 180
Mobeen Sarwar Avatar answered Mar 15 '23 15:03

Mobeen Sarwar