JavaScript How to filter records based on key click? filter records based on a combo box selection,access filter

I am C++ programmer and new to JS. I am trying to filter table based on click event. I have following table. How can i keep records based on clicking keys and filtering other records.

<!DOCTYPE html>
<html>
<body>

<table border =1 cellspacing="1" cellpadding="1" style="width:100%">
<tr><th>ID</th><th>Name</th><th>Marks</th></tr>
  <tr>
    <td>22</td>
    <td>Smith</td>      
    <td>50</td>
  </tr>
  <tr>
    <td>23</td>
    <td>Jackson</td>        
    <td>94</td>
  </tr>
  <tr>
    <td>45</td>
    <td>Doe</td>        
    <td>80</td>
  </tr>
<tr>
    <td>24</td>
    <td>Doe</td>        
    <td>80</td>
  </tr>
<tr>
    <td>25</td>
    <td>Doe</td>        
    <td>80</td>
  </tr>
<tr>
    <td>29</td>
    <td>Doe</td>        
    <td>80</td>
  </tr>
</table>

<input type="checkbox" name="vehicle" value="22"> 22<br>
<input type="checkbox" name="vehicle" value="23"> 23 <br>
<input type="checkbox" name="vehicle" value="24"> 24<br>
<input type="checkbox" name="vehicle" value="25"> 25 <br>
<input type="checkbox" name="vehicle" value="29"> 29<br>
<input type="checkbox" name="vehicle" value="45"> 45 <br>

</body>
</html>

In the above example if i click on 23 only record with id 23 should be displayed and rest should be filtered out.

Answer:1

If you're looking for a jQuery-based solution, I have whipped up one. The logic of the code is pretty simple: we trigger the filtering function whenever a change event is detected on any of the checkboxes, with a caveat:

  • if no checkboxes are checked, we show all table rows, but
  • if one or more are checked, we proceed with the filtering

For the filtering function:

  1. We first hide all table rows.
  2. Then, we map all the values of checked checkboxes into an array, say vals. This is done by using .map(), which returns the value of each element. We chain .get() after .map() in order to retrieve the array.
  3. Use the .filter() function to compare the text node in each of the table row's first table cell, to see if the text node matches any one of the elements in the array. If it does, we return them and show them.

$(function() {
  $('input[type="checkbox"]').change(function() {
    // We check if one or more checkboxes are selected
    // If one or more is selected, we perform filtering
    if($('input[type="checkbox"]:checked').length > 0) {
      // Get values all checked boxes
      var vals = $('input[type="checkbox"]:checked').map(function() {
        return this.value;
      }).get();

      // Here we do two things to table rows
      // 1. We hide all
      // 2. Then we filter, show those whose value in first <td> matches checkbox value
      $('table tr')
      .hide()    // 1
      .filter(function() {    // 2
        return vals.indexOf($(this).find('td:first').text()) > -1;
      }).show();
    } else {
      // Show all
      $('table tr').show();
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border =1 cellspacing="1" cellpadding="1" style="width:100%">
<tr><th>ID</th><th>Name</th><th>Marks</th></tr>
  <tr>
    <td>22</td>
    <td>Smith</td>      
    <td>50</td>
  </tr>
  <tr>
    <td>23</td>
    <td>Jackson</td>        
    <td>94</td>
  </tr>
  <tr>
    <td>45</td>
    <td>Doe</td>        
    <td>80</td>
  </tr>
<tr>
    <td>24</td>
    <td>Doe</td>        
    <td>80</td>
  </tr>
<tr>
    <td>25</td>
    <td>Doe</td>        
    <td>80</td>
  </tr>
<tr>
    <td>29</td>
    <td>Doe</td>        
    <td>80</td>
  </tr>
</table>

<input type="checkbox" name="vehicle" value="22"> 22<br>
<input type="checkbox" name="vehicle" value="23"> 23 <br>
<input type="checkbox" name="vehicle" value="24"> 24<br>
<input type="checkbox" name="vehicle" value="25"> 25 <br>
<input type="checkbox" name="vehicle" value="29"> 29<br>
<input type="checkbox" name="vehicle" value="45"> 45 <br>
Answer:2

You need to do it using Javascript, here a raw working example using jQuery library: http://jsfiddle.net/zn8ocm43/1/

The idea is that when you click on the row you trigger a Javascript function which hide all other rows, when you click again on the selected row the other rows are shown again.

To hide a row you can add a css class to it with

display:none

Furthermore, it would be better to separate header row from data rows using thead and tbody tags: http://www.w3schools.com/tags/tag_thead.asp

If you are willing to use jQuery you can find plugins which do all the work for you.

Answer:3

I don't know if i got it right, but i think he's wanting to filter by columns. If that's what he's searching for, here's a quick script in pure js:

function NodeListToArray (nodeList) {
	var arr = [];

	for (var i = nodeList.length >>> 0; i--;)
		arr[i] = nodeList[i];

	return arr;
}

function filter (rows, columnIndex) {
	rows.forEach(function (row, i) {
		for (var k = 0; k < rows[i].children.length; k++) {
			if (k === columnIndex || !~columnIndex) 
				rows[i].children[k].style.visibility = 'initial';
			else
				rows[i].children[k].style.visibility = 'hidden';
		}
	});
}

var rows = NodeListToArray(document.querySelectorAll('tr'));

for (var i = 0; i < rows[0].children.length; i++) {
	rows[0].children[i].addEventListener('click', function (index, e) {
		filter(rows, index);
	}.bind(null, i));
}

document.querySelector('button').addEventListener('click', function () {
	filter(rows, -1);
});
<table border =1 cellspacing="1" cellpadding="1" style="width:100%">
<tr><th>ID</th><th>Name</th><th>Marks</th></tr>
  <tr>
    <td>22</td>
    <td>Smith</td>      
    <td>50</td>
  </tr>
  <tr>
    <td>23</td>
    <td>Jackson</td>        
    <td>94</td>
  </tr>
  <tr>
    <td>45</td>
    <td>Doe</td>        
    <td>80</td>
  </tr>
<tr>
    <td>24</td>
    <td>Doe</td>        
    <td>80</td>
  </tr>
<tr>
    <td>25</td>
    <td>Doe</td>        
    <td>80</td>
  </tr>
<tr>
    <td>29</td>
    <td>Doe</td>        
    <td>80</td>
  </tr>
</table>

<button>Show All</button>
Answer:4

I want to get part of a path in URL via Angular.js and i found solution: http://mywebsite.com/one/HEREiWANT/three first i do this: app.config(function($locationProvider){ $locationProvider....

I want to get part of a path in URL via Angular.js and i found solution: http://mywebsite.com/one/HEREiWANT/three first i do this: app.config(function($locationProvider){ $locationProvider....

I am using mssql with node.js to connect to an sql server db. I am trying to reduce code by wrapping the connection code in a function with one query parameter. When I call the function from with in a ...

I am using mssql with node.js to connect to an sql server db. I am trying to reduce code by wrapping the connection code in a function with one query parameter. When I call the function from with in a ...

  1. node mysql function
  2. sql server nodes function
  3. node js mysql query function
  4. node js call mysql function
  5. node mysql query function
  6. node js mysql function
  7. node js sync function mysql
  8. node-red function mysql

How can I make each of them to fadeInUp but in sequence? demo http://jsfiddle.net/uz2rm8jy/4/ HTML <div class="c one"></div> <div class="c two"></div> <div class="c ...

How can I make each of them to fadeInUp but in sequence? demo http://jsfiddle.net/uz2rm8jy/4/ HTML <div class="c one"></div> <div class="c two"></div> <div class="c ...

  1. sequential animation with css
  2. sequential animation in android example
  3. sequential animation in qml
  4. sequential animation in powerpoint
  5. sequential animation in android

I am trying to scrape https://www.freelance.nl/opdrachten/zoeken for data using Request and Cheerio but I am running into issues posting search terms. I cannot see where the search string and ...

I am trying to scrape https://www.freelance.nl/opdrachten/zoeken for data using Request and Cheerio but I am running into issues posting search terms. I cannot see where the search string and ...

  1. find form 990
  2. find form adv
  3. find form 5500
  4. find form
  5. find form jquery
  6. find form of verb
  7. find form number mjpru
  8. find form 16 online
  9. find form number ccsu
  10. find form ltd
  11. find form 26as online
  12. find form 1040
  13. find form by name jquery
  14. find form element by name jquery
  15. find form 16
  16. find form element jquery
  17. find form input by name jquery
  18. find form 1095 a
  19. find form no. mjpru
  20. find form mjpru