Counting filtered rows of a table

How can I count the filtered rows of a table. The table has many rows. It is just normal containing many rows. With the help of following Javascript code. I filter data, by way of typing into input text, pasting the required value and also with a click to import the value from OptionList; and all these three ways are functioning perfectly. If it is possible to amend this code so as to get the count of filtered rows out of total rows, i.e. “Found … rows out of … rows”, at the top of table. I am totally a new user of javascript without any previous knowledge so sorry for that. The code is :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const trs = document.getElementById("myTable").getElementsByTagName("tr");
document.getElementById("search").addEventListener("input", filterRows);
document.getElementById("search").addEventListener("change", filterRows);
document.getElementById("search").addEventListener("click", filterRows);
function filterRows(e) {
const query = e.target.value.toLowerCase();
for (const tr of trs) {
tr.style.display = tr.innerText.toLowerCase().includes(query) ? "" : "none";
}
}
</code>
<code>const trs = document.getElementById("myTable").getElementsByTagName("tr"); document.getElementById("search").addEventListener("input", filterRows); document.getElementById("search").addEventListener("change", filterRows); document.getElementById("search").addEventListener("click", filterRows); function filterRows(e) { const query = e.target.value.toLowerCase(); for (const tr of trs) { tr.style.display = tr.innerText.toLowerCase().includes(query) ? "" : "none"; } } </code>
const trs = document.getElementById("myTable").getElementsByTagName("tr");
document.getElementById("search").addEventListener("input", filterRows);
document.getElementById("search").addEventListener("change", filterRows);
document.getElementById("search").addEventListener("click", filterRows);

function filterRows(e) {
  const query = e.target.value.toLowerCase();
  for (const tr of trs) {
    tr.style.display = tr.innerText.toLowerCase().includes(query) ? "" : "none";
  }
}

0

When you query the DOM with something like getElementsByTagName(), it returns a collection of elements. This collection has the length property, so if you wanted to know how many rows you had in total, you could just use trs.length. Though you still need to get the count that’s visible after filtering.

I would do something different in your code. I would add a class for filtered to your CSS:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.filtered {
display: none
}
</code>
<code>.filtered { display: none } </code>
.filtered {
  display: none
}

Then, in your loop:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function filterRows(e) {
const query = e.target.value.toLowerCase();
for (const tr of trs) {
tr.classList.toggle('filtered', !tr.innerText.toLowerCase().includes(query));
}
}
</code>
<code>function filterRows(e) { const query = e.target.value.toLowerCase(); for (const tr of trs) { tr.classList.toggle('filtered', !tr.innerText.toLowerCase().includes(query)); } } </code>
function filterRows(e) {
  const query = e.target.value.toLowerCase();
  for (const tr of trs) {
    tr.classList.toggle('filtered', !tr.innerText.toLowerCase().includes(query));
  }
}

That will add/remove the filtered class as appropriate.

Then, if you want the count…

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>document.querySelectorAll('#myTable tr:not(.filtered)').length
</code>
<code>document.querySelectorAll('#myTable tr:not(.filtered)').length </code>
document.querySelectorAll('#myTable tr:not(.filtered)').length

One final note… I’d recommend avoiding the usage of IDs. They clutter up the global namespace and can cause conflicts when you add code later on.

7

Details are commented in the example.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// Reference <input type="search">
const srch = document.querySelector("#search");
// Reference <output>
const msgs = document.querySelector("#message");
// Gather all <tr> into a NodeList
const rows = document.querySelectorAll("tr");
/**
* This event handler matches the text to the text of each
* <tr> while the user is typing.
* @param {object} e - Event object
*/
const findTerm = e => {
// Convert search term to lower case
let term = e.target.value.toLowerCase();
// Iterate through the <tr>
rows.forEach(tr => {
// On each <tr> convert it's text to lower case
let text = tr.textContent.toLowerCase();
// If a match is found...
if (text.includes(term)) {
// remove the .hidden class from <tr>...
tr.classList.remove("hidden");
// otherwise add the .hidden class to <tr>
} else {
tr.classList.add("hidden");
}
});
// Get the amount of tr.hidden
let hid = document.querySelectorAll(".hidden").length;
/*
If there at least one tr.hidden, display a message
announcing the totals, otherwise display nothing.
*/
msgs.value = hid > 0 ? `${rows.length - hid}
out of ${rows.length} matched the term: "${term}"` : "";
};
/*
Register input#search to the "input" event.
Registering it to the "change" and "click" events is
pointless, "input" event covers what "change" and
"click" does as well.
*/
srch.addEventListener("input", findTerm);
</code>
<code>// Reference <input type="search"> const srch = document.querySelector("#search"); // Reference <output> const msgs = document.querySelector("#message"); // Gather all <tr> into a NodeList const rows = document.querySelectorAll("tr"); /** * This event handler matches the text to the text of each * <tr> while the user is typing. * @param {object} e - Event object */ const findTerm = e => { // Convert search term to lower case let term = e.target.value.toLowerCase(); // Iterate through the <tr> rows.forEach(tr => { // On each <tr> convert it's text to lower case let text = tr.textContent.toLowerCase(); // If a match is found... if (text.includes(term)) { // remove the .hidden class from <tr>... tr.classList.remove("hidden"); // otherwise add the .hidden class to <tr> } else { tr.classList.add("hidden"); } }); // Get the amount of tr.hidden let hid = document.querySelectorAll(".hidden").length; /* If there at least one tr.hidden, display a message announcing the totals, otherwise display nothing. */ msgs.value = hid > 0 ? `${rows.length - hid} out of ${rows.length} matched the term: "${term}"` : ""; }; /* Register input#search to the "input" event. Registering it to the "change" and "click" events is pointless, "input" event covers what "change" and "click" does as well. */ srch.addEventListener("input", findTerm); </code>
// Reference <input type="search">
const srch = document.querySelector("#search");
// Reference <output>
const msgs = document.querySelector("#message");
// Gather all <tr> into a NodeList
const rows = document.querySelectorAll("tr");

/**
 * This event handler matches the text to the text of each
 * <tr> while the user is typing.
 * @param {object} e - Event object
 */
const findTerm = e => {
  // Convert search term to lower case
  let term = e.target.value.toLowerCase();
  // Iterate through the <tr>
  rows.forEach(tr => {
    // On each <tr> convert it's text to lower case
    let text = tr.textContent.toLowerCase();
    // If a match is found...
    if (text.includes(term)) {
      // remove the .hidden class from <tr>...
      tr.classList.remove("hidden");
      // otherwise add the .hidden class to <tr>
    } else {
      tr.classList.add("hidden");
    }
  });
  // Get the amount of tr.hidden
  let hid = document.querySelectorAll(".hidden").length;
  /* 
    If there at least one tr.hidden, display a message 
    announcing the totals, otherwise display nothing.
  */
  msgs.value = hid > 0 ? `${rows.length - hid} 
  out of ${rows.length} matched the term: "${term}"` : "";
};
/*
  Register input#search to the "input" event.
  Registering it to the "change" and "click" events is
  pointless, "input" event covers what "change" and 
  "click" does as well.
*/
srch.addEventListener("input", findTerm);
  
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>:root {
font: 2ch/1.2 "Segoe UI"
}
table,
td {
margin: 5px auto;
border: 2px solid #000;
text-align: center;
}
input,
output {
display: block;
height: 1.5rem;
margin: 5px auto;
font: inherit;
text-align: center;
}
.hidden {
visibility: collapse;
}</code>
<code>:root { font: 2ch/1.2 "Segoe UI" } table, td { margin: 5px auto; border: 2px solid #000; text-align: center; } input, output { display: block; height: 1.5rem; margin: 5px auto; font: inherit; text-align: center; } .hidden { visibility: collapse; }</code>
:root {
  font: 2ch/1.2 "Segoe UI"
}

table,
td {
  margin: 5px auto;
  border: 2px solid #000;
  text-align: center;
}

input, 
output {
  display: block;
  height: 1.5rem;
  margin: 5px auto;
  font: inherit;
  text-align: center;
}

.hidden {
  visibility: collapse;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><input id="search" type="search">
<output id="message"></output>
<table>
<tbody><tr><td>Microsoft</td></tr><tr><td> Google</td></tr><tr><td> Apple</td></tr><tr><td> Amazon</td></tr><tr><td> Facebook</td></tr><tr><td> Netflix</td></tr><tr><td> Adobe</td></tr><tr><td> Salesforce</td></tr><tr><td> Oracle</td></tr><tr><td> IBM</td></tr><tr><td> Intel</td></tr><tr><td> Cisco</td></tr><tr><td> HP</td></tr><tr><td> Dell</td></tr><tr><td> VMware</td></tr><tr><td> Twitter</td></tr><tr><td> Uber</td></tr><tr><td> Airbnb</td></tr><tr><td> Dropbox</td></tr><tr><td> Slack</td></tr><tr><td> Tesla</td></tr><tr><td> SpaceX</td></tr><tr><td> Reddit</td></tr><tr><td> Pinterest</td></tr><tr><td> Zoom</td></tr><tr><td> Shopify</td></tr><tr><td> Square</td></tr><tr><td> PayPal</td></tr><tr><td> LinkedIn</td></tr><tr><td> Etsy</td></tr></tbody></table></code>
<code><input id="search" type="search"> <output id="message"></output> <table> <tbody><tr><td>Microsoft</td></tr><tr><td> Google</td></tr><tr><td> Apple</td></tr><tr><td> Amazon</td></tr><tr><td> Facebook</td></tr><tr><td> Netflix</td></tr><tr><td> Adobe</td></tr><tr><td> Salesforce</td></tr><tr><td> Oracle</td></tr><tr><td> IBM</td></tr><tr><td> Intel</td></tr><tr><td> Cisco</td></tr><tr><td> HP</td></tr><tr><td> Dell</td></tr><tr><td> VMware</td></tr><tr><td> Twitter</td></tr><tr><td> Uber</td></tr><tr><td> Airbnb</td></tr><tr><td> Dropbox</td></tr><tr><td> Slack</td></tr><tr><td> Tesla</td></tr><tr><td> SpaceX</td></tr><tr><td> Reddit</td></tr><tr><td> Pinterest</td></tr><tr><td> Zoom</td></tr><tr><td> Shopify</td></tr><tr><td> Square</td></tr><tr><td> PayPal</td></tr><tr><td> LinkedIn</td></tr><tr><td> Etsy</td></tr></tbody></table></code>
<input id="search" type="search">
<output id="message"></output>
<table>
<tbody><tr><td>Microsoft</td></tr><tr><td> Google</td></tr><tr><td> Apple</td></tr><tr><td> Amazon</td></tr><tr><td> Facebook</td></tr><tr><td> Netflix</td></tr><tr><td> Adobe</td></tr><tr><td> Salesforce</td></tr><tr><td> Oracle</td></tr><tr><td> IBM</td></tr><tr><td> Intel</td></tr><tr><td> Cisco</td></tr><tr><td> HP</td></tr><tr><td> Dell</td></tr><tr><td> VMware</td></tr><tr><td> Twitter</td></tr><tr><td> Uber</td></tr><tr><td> Airbnb</td></tr><tr><td> Dropbox</td></tr><tr><td> Slack</td></tr><tr><td> Tesla</td></tr><tr><td> SpaceX</td></tr><tr><td> Reddit</td></tr><tr><td> Pinterest</td></tr><tr><td> Zoom</td></tr><tr><td> Shopify</td></tr><tr><td> Square</td></tr><tr><td> PayPal</td></tr><tr><td> LinkedIn</td></tr><tr><td> Etsy</td></tr></tbody></table>

6

In your code you’re using for loop. You can define a variable named counter and in that for loop you can increase the counter’s value by 1 in each iteration. And finally you can show that data.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const trs = document.getElementById("myTable").getElementsByTagName("tr")
document.getElementById("search").addEventListener("input", filterRows)
document.getElementById("search").addEventListener("change", filterRows)
document.getElementById("search").addEventListener("click", filterRows)
function filterRows(e) {
let counter = 0;
const query = e.target.value.toLowerCase()
for (const tr of trs) {
counter++;
tr.style.display = tr.innerText.toLowerCase().includes(query) ? "" : "none"
}
document.querySelector("#output").innerText = counter;
}</code>
<code>const trs = document.getElementById("myTable").getElementsByTagName("tr") document.getElementById("search").addEventListener("input", filterRows) document.getElementById("search").addEventListener("change", filterRows) document.getElementById("search").addEventListener("click", filterRows) function filterRows(e) { let counter = 0; const query = e.target.value.toLowerCase() for (const tr of trs) { counter++; tr.style.display = tr.innerText.toLowerCase().includes(query) ? "" : "none" } document.querySelector("#output").innerText = counter; }</code>
const trs = document.getElementById("myTable").getElementsByTagName("tr")
document.getElementById("search").addEventListener("input", filterRows)
document.getElementById("search").addEventListener("change", filterRows)
document.getElementById("search").addEventListener("click", filterRows)

function filterRows(e) {
  let counter = 0;
  const query = e.target.value.toLowerCase()
  for (const tr of trs) {
    counter++;
    tr.style.display = tr.innerText.toLowerCase().includes(query) ? "" : "none"
  }
  
  document.querySelector("#output").innerText = counter;
}

2

This code filters a table to value of inputs and shows rows and count.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>let tableHNDL=document.querySelector("#myTable");
document.getElementById("searchValue").addEventListener("input", filterRows);
document.getElementById("selectOptionList").addEventListener("change", filterRows);
document.getElementById("searchBTN").addEventListener("click", filterRows);
function filterRows(e) {
let searchFor=e.target.value.toLowerCase(); // gets the elements value. selectOption & textbox & button.
//let searchFor=document.querySelector("#searchValue").value;
let rowsFound=0;
for(let a=0;a<tableHNDL.rows.length;a++){
for(let b=0;b<tableHNDL.rows[a].cells.length;b++){
if(tableHNDL.rows[a].cells[b].innerHTML.toLowerCase()==searchFor)
{tableHNDL.rows[a].style.display="block";rowsFound++;break;}
else
{tableHNDL.rows[a].style.display="none";}
}
}
let rs=document.querySelector("#resultCount");
if(rowsFound){rs.innerHTML=rowsFound+" rows found.";}else{rs.innerHTML="Nothing found.";}
}</code>
<code>let tableHNDL=document.querySelector("#myTable"); document.getElementById("searchValue").addEventListener("input", filterRows); document.getElementById("selectOptionList").addEventListener("change", filterRows); document.getElementById("searchBTN").addEventListener("click", filterRows); function filterRows(e) { let searchFor=e.target.value.toLowerCase(); // gets the elements value. selectOption & textbox & button. //let searchFor=document.querySelector("#searchValue").value; let rowsFound=0; for(let a=0;a<tableHNDL.rows.length;a++){ for(let b=0;b<tableHNDL.rows[a].cells.length;b++){ if(tableHNDL.rows[a].cells[b].innerHTML.toLowerCase()==searchFor) {tableHNDL.rows[a].style.display="block";rowsFound++;break;} else {tableHNDL.rows[a].style.display="none";} } } let rs=document.querySelector("#resultCount"); if(rowsFound){rs.innerHTML=rowsFound+" rows found.";}else{rs.innerHTML="Nothing found.";} }</code>
let tableHNDL=document.querySelector("#myTable");
document.getElementById("searchValue").addEventListener("input", filterRows);
document.getElementById("selectOptionList").addEventListener("change", filterRows);
document.getElementById("searchBTN").addEventListener("click", filterRows);

function filterRows(e) {
    let searchFor=e.target.value.toLowerCase(); // gets the elements value. selectOption & textbox & button.
    //let searchFor=document.querySelector("#searchValue").value;
    let rowsFound=0;

for(let a=0;a<tableHNDL.rows.length;a++){
for(let b=0;b<tableHNDL.rows[a].cells.length;b++){
if(tableHNDL.rows[a].cells[b].innerHTML.toLowerCase()==searchFor)
{tableHNDL.rows[a].style.display="block";rowsFound++;break;}
else
{tableHNDL.rows[a].style.display="none";}
}
}

let rs=document.querySelector("#resultCount");
if(rowsFound){rs.innerHTML=rowsFound+" rows found.";}else{rs.innerHTML="Nothing found.";}

}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><table border="1" id="myTable">
<tr><td>a1</td><td>b1</td><td>c1</td></tr>
<tr><td>a2</td><td>b2</td><td>c2</td></tr>
<tr><td>a3</td><td>b4</td><td>c3</td></tr>
<tr><td>a4</td><td>b4</td><td>c4</td></tr>
</table>
<select id="selectOptionList">
<option value="c4">c4</option>
<option value="a2">a2</option>
<option value="b1">b1</option>
<option value="c3">c3</option>
</select>
<input id="searchValue" type="text"/>
<input id="searchBTN" type="button" value="a2"/>
<div id="resultCount"></div></code>
<code><table border="1" id="myTable"> <tr><td>a1</td><td>b1</td><td>c1</td></tr> <tr><td>a2</td><td>b2</td><td>c2</td></tr> <tr><td>a3</td><td>b4</td><td>c3</td></tr> <tr><td>a4</td><td>b4</td><td>c4</td></tr> </table> <select id="selectOptionList"> <option value="c4">c4</option> <option value="a2">a2</option> <option value="b1">b1</option> <option value="c3">c3</option> </select> <input id="searchValue" type="text"/> <input id="searchBTN" type="button" value="a2"/> <div id="resultCount"></div></code>
<table border="1" id="myTable">
<tr><td>a1</td><td>b1</td><td>c1</td></tr>
<tr><td>a2</td><td>b2</td><td>c2</td></tr>
<tr><td>a3</td><td>b4</td><td>c3</td></tr>
<tr><td>a4</td><td>b4</td><td>c4</td></tr>
</table>

<select id="selectOptionList">
<option value="c4">c4</option>
<option value="a2">a2</option>
<option value="b1">b1</option>
<option value="c3">c3</option>
</select>

<input id="searchValue" type="text"/>

<input id="searchBTN" type="button" value="a2"/>

<div id="resultCount"></div>
New contributor

AmirABD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật