Luke Prior
Luke Prior

Reputation: 967

Jquery filter table if fits into input range

Question:

I have a HTML with type number i want to ge the value using jQuery create a range by adding 10% on either side then checking if each row falls in the range if not hide the row.

Code:

HTML

<input type="number" id="myPrice" onkeyup="myPriceFunction()" placeholder="Enter amount.." title="Type in a amount" min="0">

JavaScript/jQuery

$(document).ready(function(){
  $("#myPrice").on("keyup", function() {
       priceLow = $(this).val() * 0.9;
       priceHigh = $(this).val() * 1.1;
  });
}); 

JS fiddle

https://jsfiddle.net/nx30zqjd/7/

Other:

I am using:

.addClass('discarded').hide();

.removeClass('discarded').show();

to hide and show rows

Expected Results:

I want the range to be created with priceLow and priceHigh then get price column get rid of $ and check if it falls within range if not hide using code above.

Update:

I added

    $(this).closest('tr').removeClass('discarded').show();
  } else {
    $(this).closest('tr').addClass('discarded').hide();
  }

however then this shows nothing because i don't check discarded rows, if i remove the check for discarded rows it doesn't apply my search to the range not sure if there is a easy way to have both but it seems to be ok for the moment

If you need any more information please ask.

Thanks

Upvotes: 1

Views: 103

Answers (1)

Just code
Just code

Reputation: 13801

You can use this, you just need to iterate and get the values and then check, based on that you can hide and show.

$("#myPrice").on("keyup", function() {
    if ($(this).val() === '') {
      $("#myTable tr").show();
      return;
    }
    priceLow = $(this).val() * 0.9;
    priceHigh = $(this).val() * 1.1;
    $("#myTable tr td:nth-child(2)").each(function(e) {
      var value = this.textContent.replace('$', '');
      if (value >= priceLow && value <= priceHigh) {
        $(this).closest('tr').show();
      } else {
        $(this).closest('tr').hide();
      }
    })
  });

$(document).ready(function() {

});
$(document).ready(function() {
  $("#myPrice").on("keyup", function() {
    if ($(this).val() === '') {
      $("#myTable tr").show();
      return;
    }
    priceLow = $(this).val() * 0.9;
    priceHigh = $(this).val() * 1.1;
    $("#myTable tr td:nth-child(2)").each(function(e) {
      var value = parseFloat(this.textContent.replace('$', ''));
      if (value >= priceLow && value <= priceHigh) {      
        $(this).closest('tr').removeClass('discarded').show();        
      } else {
        $(this).closest('tr').addClass('discarded').hide();        
      }
    })
  });
});
<style>* {
  box-sizing: border-box;
}

#mySearch {
  background-image: url('https://www.w3schools.com/css/searchicon.png');
  background-position: 10px 10px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myTable {
  border-collapse: collapse;
  width: 100%;
  border: 1px solid #ddd;
  font-size: 18px;
}

#myTable th,
#myTable td {
  text-align: left;
  padding: 12px;
}

#myTable tr {
  border-bottom: 1px solid #ddd;
}

.show {
  display: block;
}

a {
  color: blue;
  text-decoration: none;
  /* no underline */
}

</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h2>CPUs</h2>
<a href="index.php">CPU</a>
<a href="mobo.php">Motherboards</a>
<input type="number" id="myPrice"  placeholder="Enter amount.." title="Type in a amount" min="0">
<input type="text" id="mySearch"  placeholder="Search for cpus.." title="Type in a cpu name">

<table id='myTable'>
  <thead>
    <tr>
      <th>CPU</th>
      <th>Price</th>
      <th>Mark</th>
      <th>Value</th>
      <th>Socket</th>
      <th>Image</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a href='mobo.php?cpu_name=AMD Ryzen 5 2600"' target='_blank'>AMD Ryzen 5 2600</a></td>
      <td>$246.05</td>
      <td>13537</td>
      <td>55.02</td>
      <td>AM4</td>
      <td><img src=NA height='42' width='42'></td>
    </tr>
  </tbody>
  <tbody>
    <tr>
      <td><a href='mobo.php?cpu_name=Intel Core i7-8700K"' target='_blank'>Intel Core i7-8700K</a></td>
      <td>$585.90</td>
      <td>15957</td>
      <td>27.24</td>
      <td>LGA1151</td>
      <td><img src=". $row[" url "]." height='42' width='42'></td>
    </tr>
  </tbody>
</table>

Upvotes: 1

Related Questions