I used w3School code for my page and it works fine but it only filters one column, don’t know how create loops but hopping there is easier solution.
td =
Adding || and adding the new condition under if statements totally worked.
function myFunction() {
var input, filter, table, tr, td, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0]; // for column one
td1 = tr[i].getElementsByTagName("td")[1]; // for column two
/* ADD columns here that you want you to filter to be used on */
if (td) {
if ( (td.innerHTML.toUpperCase().indexOf(filter) > -1) || (td1.innerHTML.toUpperCase().indexOf(filter) > -1) ) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
* {
box-sizing: border-box;
}
#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ffffd;
margin-bottom: 12px;
}
#myTable {
border-collapse: collapse;
width: 100%;
border: 1px solid #ffffd;
font-size: 18px;
}
#myTable th,
#myTable td {
text-align: left;
padding: 12px;
}
#myTable tr {
border-bottom: 1px solid #ffffd;
}
#myTable tr.header,
#myTable tr:hover {
background-color: #f1f1f1;
}
<h2>My Customers</h2>
<input type="text" id="myInput" onkeyup="myFunction()"
placeholder="Search for names.." title="Type in a name">
<table id="myTable">
<tr class="header">
<th style="width:60%;">Name</th>
<th style="width:40%;">Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbkop</td>
<td>Sweden</td>
</tr>
<tr>
<td>Island Trading</td>
<td>UK</td>
</tr>
<tr>
<td>Koniglich Essen</td>
<td>Germany</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Italy</td>
</tr>
<tr>
<td>North/South</td>
<td>UK</td>
</tr>
<tr>
<td>Paris specialites</td>
<td>France</td>
</tr>
</table>
Based on probably the same tutorial that you followed, to take in three different text inputs and sort all 's in the row accordingly:
function SortByMultiple() {
//Declare needed variables
var dateInput, nameInput, locationInput, dateFilter, nameFilter, locationFilter, table, tr, tdN, tdD, tdL, i;
//Set inputs by getElementById
nameInput = document.getElementById('nameInput');
dateInput = document.getElementById('dateInput');
locationInput = document.getElementById('locationInput');
//Set filters
nameFilter = nameInput.value.toUpperCase();
dateFilter = dateInput.value.toUpperCase();
locationFilter = locationInput.value.toUpperCase();
//Set the table and tr variables
table = document.getElementById("UL");
tr = document.getElementsByTagName("tr");
//Loop through items and hide those that don't match the query -->
for (i = 0; i < tr.length; i++) {
//Name is at index 0
tdN = tr[i].getElementsByTagName("td")[0];
//Date is at index 2
tdD = tr[i].getElementsByTagName("td")[2];
//Location is at index 1
tdL = tr[i].getElementsByTagName("td")[1];
if (tdN.innerHTML.toUpperCase().indexOf(nameFilter) > -1 && tdD.innerHTML.toUpperCase().indexOf(dateFilter) > -1 && tdL.innerHTML.toUpperCase().indexOf(locationFilter) > -1) {
tr[i].style.display = "";
}
else {
tr[i].style.display = "none";
}
}
}
By setting the conditions after each others with AND they will all be taken into account in the filter!
Add this JQuery in the head tag of the file. The #myinput
is the id search box which is an input element of type text.
The #tab-id
is the id of the tbody, so you should explicitly divide the table into thead and tbody
<script>
$(document).ready(function(){
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#tab-id tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1);
});
});
});
</script>
This solution is for multi column filter with multiple values
The empty text boxes is to type your value to filter
Class name for all the text boxes are '.filter_rep'
$(document).on('keyup','.filter_rep',function () {
var $this = $(this).val();
var val_array = [];
var id_array = [];
$('.filter_rep').each(function () {
thisval = $(this).val();
id = '.'+$(this).attr('id');
if (thisval){
id_array.push(id);
val_array.push(thisval);
}
});
// using val_array to check all the columns and hide
if(val_array.length > 0){
$('tr').each(function () {
$(this).show();
});
for (i=0;i<val_array.length;i++){
$(id_array[i]).each(function () {
if (!($(this).text().toLowerCase().indexOf(val_array[i]) > -1)){
$(this).parent().hide();
}
});
}
}else{
$('tr').each(function () {
$(this).show();
});
}
})
});
If any clarifications ping me!
Borrowing code from this post I am able to filter on 2 columns using the || (Or) operator. However, I'd like to be able to filter using the && (And) operator.
I have been unsuccessful in my multiple attempts. I could use some help.
<script>
function myFunction() {
var input0, input1, filter0, filter1, table, tr, td, cell, i, j;
document.getElementById("myInput0").value = 'Female';
document.getElementById("myInput1").value = 'Engineering';
input0 = document.getElementById("myInput0");
input1 = document.getElementById("myInput1");
filter0 = input0.value.toUpperCase();
filter1 = input1.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 1; i < tr.length; i++) {
// Hide the row initially.
tr[i].style.display = "none";
td = tr[i].getElementsByTagName("td");
for (var j = 0; j < td.length; j++) {
cell = tr[i].getElementsByTagName("td")[j];
if (cell) {
if (cell.textContent.toUpperCase().indexOf(filter0)>-1 ||
cell.textContent.toUpperCase().indexOf(filter1)>-1) {
tr[i].style.display = "";
break;
}
}
}
}
}
</script>
<body>
<input type="text" id="myInput0">
<input type="text" id="myInput1">
<input type='button' onclick='myFunction()' value='click me' />
<table id="myTable">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Gender</th>
<th>Department</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>Male</td>
<td>Sales</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>Female</td>
<td>Service</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>Female</td>
<td>Service</td>
</tr>
<tr>
<td>Anja</td>
<td>Ravendale</td>
<td>Female</td>
<td>Engineering</td>
</tr>
<tr>
<td>Thomas</td>
<td>Dubois</td>
<td>Male</td>
<td>Sales</td>
</tr>
<tr>
<td>Deidre</td>
<td>Masters</td>
<td>Female</td>
<td>Sales</td>
</tr>
<tr>
<td>Sean</td>
<td>Franken</td>
<td>Male</td>
<td>Engineering</td>
</tr>
</tbody>
</table>
</body>
Enhanced Rob.M's answer to suit multi-column search with below code.
function filterTable(event) {
//Convert search value to uppercase and assign the value to variable 'filter'
var filter = event.target.value.toUpperCase();
//Find total rows in the table's body
var rows = document.querySelector("#table tbody").rows;
//Start looping rows
for (var i = 0; i < rows.length; i++) {
//Count the columns in current row
var colCount = $("#table th").length;
//Assign a variable 'res' for result counting
var res = 1;
//Start looping columns in current row
for (let j = 0; j < colCount; j++) {
//Iterate single cell and convert the contents to uppercase, assign the content to variable 'x'
var x = "col_" + j;
var x = rows[i].cells[j].textContent.toUpperCase();
//find the position of first index of the search value.
//If search value found ADD 1 to 'res' else LESS 1
x.indexOf(filter) > -1 ? res++ : res--;
}
//Convert the colCount variable to absolute value
colCount = -Math.abs(colCount - 1);
//Display or hide the row, based on result condition
res > colCount || filter == "" ? (rows[i].style.display = "") : (rows[i].style.display = "none");
}
}