I've been working on filtering and searching the table below based on the href
value. My code successfully filters the displayed columns in the table, but it fails to work when I search for a string within the href attribute.
For instance, if I search for "Google" in the search bar, I want to display the row that contains the href
value of https://www.google.com
.
I've searched everywhere for a solution, but haven't been able to find one. Can anyone offer assistance or guide me to where I might discover the answer?
Moreover, is there a way to link parent rows with child rows in the table without using the id
attribute? This way, when the filter returns a child row, I also want to display the corresponding parent row.
Here's my code on JsFiddle - JsFiddle
HTML :
<body>
<div>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for website..">
<button type="submit">Search</button>
</div>
<table border="0" id="myTable">
<thead hidden>
<tr>
<th>Name</th>
<th>URL</th>
</tr>
</thead>
<tbody>
<tr class="header clickable-row" style="cursor:pointer" data-toggle="true">
<td colspan="2">
<label style="cursor:pointer">Parent Row 1</label>
</td>
</tr>
<tr class="hide collapse" style="display:none">
<td>
<h4>Column1</h4>
</td>
<td>
<a class="connectorlink" href="https://google.com" target="_blank">website</a>
</td>
</tr>
<tr class="hide collapse" style="display:none">
<td>
<h4>Column1</h4>
</td>
<td>
<a class="connectorlink" href="https://yahoo.com" target="_blank">website</a>
</td>
</tr>
</tbody>
</table>
</body>
JavaScript
$(document).ready(function() {
$('tr.header').click(function() {
$(this).find('span').text(function(_, value) {});
$(this).nextUntil('tr.header').slideToggle(100, function() {});
});
});
function myFunction()
{
// Declare variables
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
// Loop through all table rows, and hide those that don't match the search query
for (i = 0; i < tr.length; i++)
{
td = tr[i];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1)
{
tr[i].style.display = "";
}
else
{
tr[i].style.display = "none";
}
}
}
}
CSS
table,
tr,
td,
th {
border-collapse: collapse;
}
h4,
{
margin: 0;
padding: 0;
font-family: Inter;
font-style: normal;
font-size: 20px;
font-weight: 600;
line-height: 28px;
}
label {
padding: 10px 10px 10px 12px;
}
.tabledesign {
width: 900px;
border-collapse: separate;
border-spacing: 0 15px;
margin: 50px auto;
}
th {
background: #e5e5e5;
color: rgba(0, 0, 0, 0.84);
font-weight: bold;
}
td,
th {
padding: 10px;
border: 1px solid #ccc;
text-align: left;
font-size: 18px;
}
#myInput {
display: flex;
background: #FFFFFF;
font-family: Inter;
/* Add a search icon to input */
background-position: 10px 12px;
/* Position the search icon */
background-repeat: no-repeat;
/* Do not repeat the icon image */
width: 795px;
/* Full-width */
height: 46px;
font-size: 16px;
/* Increase font-size */
padding: 12px 20px 12px 40px;
/* Add some padding */
border: 1px solid #ddd;
/* Add a grey border */
margin-bottom: 12px;
/* Add some space below the input */
}
* {
box-sizing: border-box;
}