Addy2311 Addy2311 - 9 days ago 4
CSS Question

Filter/search table doesn't work

I've created a page where I can save actors. They are stored in an array.
And after saving these data, I'm creating a table where I'm inserting the data.

I wanted to create a search element where I can filter my data by name. But it didn't work.

Maybe someone of you knows why.



window.onload = function() {
var allArtists = [];

$('#submit').click(function() {
var $rowTemplate = $('<tr><td data-id="id"></td><td data-id="name"></td><td data-id="geburtsort"></td><td data-id="geburtsdatum"></td><td data-id="favorite"></td></tr>');

var artistName = $("#name").val();
var ort = $("#ort").val();
var datum = $("#datum").val();
var favourite = $("[name=Favorit]").is(':checked');

allArtists.push([artistName, ort, datum]);

var rowId = allArtists.length;

$rowTemplate.find('[data-id=id]').text(rowId);
$rowTemplate.find('[data-id=name]').text(artistName);
$rowTemplate.find('[data-id=geburtsort]').text(ort);
$rowTemplate.find('[data-id=geburtsdatum]').text(datum);

var checked = favourite ? "checked" : "";

$rowTemplate.find('[data-id=favorite]').text(favourite).append($('<input type="checkbox" id="fave" ' + checked + '>'));
$("#table tbody").append($rowTemplate);
});
};

function myFunction() {
var input, filter, table, tr, td, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("table");
tr = table.getElementsByTagName("tr");

// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}

body {
font-family: Arial, Helvetica;
font-size: 13px;
}


#myInput {
background-position: 10px 12px;
background-repeat: no-repeat;
width: 20%;
font-size: 16px;
padding: 5px;
border: 1px solid #ddd;
margin-bottom: 12px;
}

#table {
border-collapse: collapse;
width: 60%;
border: 1px solid #ddd;
font-size: 14px;
}

#table th, #myTable td {
text-align: left;
padding: 12px;
}

#table tr {

border-bottom: 1px solid #ddd;
}

#table tr.header, #table tr:hover {

background-color: #f1f1f1;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Artist</title>
</head>
<script src="js/jquery-3.1.1.min.js"></script>
<script src="js/main.js"></script>
<link rel="stylesheet" href="css/style.css">
<body>
<h1>Künstler hinzufügen</h1>
<form id="send">
<label>Name des Künstlers</label>
<br>
<input id="name" type="text" placeholder="Name des Künstlers" />
<br>

<label>Ort</label>
<br>
<input id="ort" type="text" placeholder="Woher stammt der Künstler" />
<br>

<label>Geburtsdatum</label>
<br>
<input id="datum" type="text" placeholder="Wann ist der Künstler geboren?" />
<br>
</form>

<p>
<input type="checkbox" name="Favorit" value="Favorit">Favorit
<p>

<input type="button" id="submit" name="senden" value="Senden">

<input type="text" id="myInput" onkeyup="myFunction()" placeholder=" Search for names..">

<table id="table">
<tbody>
<tr>
<th>ID</th>
<th>Name</th>
<th>Geburtsort</th>
<th>Geburtsdatum</th>
<th>Favorit</th>
</tr>
</tbody>


</table>
</body>
</html>




Answer

In function myFunction

change

td = tr[i].getElementsByTagName("td")[0];

to

td = tr[i].getElementsByTagName("td")[1];

You wanna check the name, use [0] check the id

Comments