Skip to content

Instantly share code, notes, and snippets.

@mutuadavid93
Created July 10, 2018 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mutuadavid93/3c0a12bc31f4f464a70a4b1dac7fc313 to your computer and use it in GitHub Desktop.
Save mutuadavid93/3c0a12bc31f4f464a70a4b1dac7fc313 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
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 #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;
}
#myTable tr.header, #myTable tr:hover {
background-color: #f1f1f1;
}
</style>
</head>
<body>
<h2>My Customers</h2>
<input type="text" id="myInput" name="appSearchBox" placeholder="Search for names.." title="Type in a name">
<table id="myTable">
<tr class="header">
<th style="width:60%;">ColumnName</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>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" type="text/javascript"></script>
<script>
$(function() {
var $mySearchFunc = function($tblID, $inputID, $SearchByColName) {
// Try to get the th index in order to target td index.
$('#'+$tblID+' tr:eq(0) th').each(function(index, item) {
var $tblid = $(this).closest('table').attr('id');
if($(this).text() === $SearchByColName){
// The Search Logic.
$('#'+$inputID).on('keyup', function() {
var $mykey = $(this);
myFunction($tblid, $mykey.attr('id'), index); // Search.
});
} // End if.
});
}; // invoke ASAP!
// Invoke using this Line Anywhere You Want a Search To Work using
// you Custom Table Id, TextField Id and Column Name.
$mySearchFunc('myTable', 'myInput', 'ColumnName');
}); // docx ready
// The Search Method:
function myFunction(tableid, elemID, td_index) {
var input, filter, table, tr, td, i;
input = document.getElementById(elemID);
filter = input.value.toUpperCase();
table = document.getElementById(tableid);
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[td_index];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
} // if
} // for loop
} // myFunction
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment