Można tworzyć za każdym razem nowy fragment tabeli (body) z odpowiednimi wartościami i kasować to co było wcześniej. Na przykładzie filtrowanie graczy powyżej jakiejś ilości punktów.
1) index.html (z js)
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Table score</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='main.css'>
</head>
<body>
<div>
<label>select min score</label>
<input id='input-score' type='number' min='0'>
<table class='table'>
<thead>
<td>
id
</td>
<td>
name
</td>
<td>
score
</td>
</thead>
<tbody id='table-body'>
</tbody>
</table>
</div>
<script>
const playersArray = [
{
id: 1,
name: 'pierwszy',
score: 120
},
{
id: 2,
name: 'drugi',
score: 180
},
{
id: 3,
name: 'trzeci',
score: 160
},
{
id: 4,
name: 'czwarty',
score: 80
},
{
id: 5,
name: 'piąty',
score: 110
},
{
id: 6,
name: 'szósty',
score: 150
}
]
const tableBody = document.querySelector('#table-body');
const inputScore = document.querySelector('#input-score');
inputScore.addEventListener('change', filtr);
function filtr() {
const minScore = document.querySelector('#input-score').value;
while(tableBody.firstChild){
tableBody.firstChild.remove();
}
for(i=0;i<playersArray.length;i++) {
if(playersArray[i].score > parseInt(minScore)) {
const row = document.createElement('tr');
const idCol = document.createElement('td');
idCol.innerHTML = playersArray[i].id;
const nameCol = document.createElement('td');
nameCol.innerHTML = playersArray[i].name;
const scoreCol = document.createElement('td');
scoreCol.innerHTML = playersArray[i].score;
row.appendChild(idCol);
row.appendChild(nameCol);
row.appendChild(scoreCol);
tableBody.appendChild(row);
}
}
}
</script>
</body>
</html>
2) style.css (dodanie obramowania do tabeli)
table {
border-collapse: collapse;
}
table, thead, td {
border: 1px solid black;
}