Front End Filter Using Array

Apply filtr on table with js

by Mobeen Sarwar

HTML

<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="70">70</option>                                           <option value="200">200</option>                                         <option value="0">0</option>
                        <option value="0">0</option>
                        <option value="0">0</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="12">12</option>                                               <option value="120">120</option>
                        <option value="0">0</option>
                        <option value="0">0</option>
                        <option value="0">0</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">
              ...

JavaScript

$('#filter').click(function () {
  var hidden_rows=0,tdScoring=[],tdEarning=[];
  var scoring=$('#scoring1').val();
  var earning = $('#earning1').val();
  table = document.getElementById('mastermindTable');
  tr = table.getElementsByTagName("tr");
  var rows=tr.length;
  rows-=2;
 document.getElementById('noRecordTR').style.display = 'none';
 tdEarning[0]='';
 tdScoring[0]='';
 for (i = 2; i < tr.length; i++) {
   tdEarning[1] = tr[i].getElementsByTagName("td")[3].innerText;
   tdScoring[1] = tr[i].getElementsByTagName("td")[4].innerText;
  if (tdScoring.indexOf(scoring) >-1 && tdEarning.indexOf(earning) >-1) {
   tr[i].style.display = '';  
  }
  else {
   tr[i].style.display = 'none';
   ++hidden_rows;
  }
 }
if(hidden_rows==rows){
 for (i = 2; i < tr.length; i++) {
  tr[i].style.display = 'none';
 }
 document.getElementById('noRecordTR').style.display = '';
}
});