Greetings again everyone
Recently i´ve been asked to develop a script to allow filtering in the content of the table, with dinamic options based on the own content. Example: a table with the name of some students and their respective numbers, and then you wanna show only studentes called "Joao", or students with number "5", or even only students called "joao" with number "5".
The structure we are going to use is a basic html table, like:
-
<table>
-
<thead>
-
<tr>
-
<th>Header</th>
-
</tr>
-
</thead>
-
<tbody>
-
<tr>
-
<td>Content</td>
-
</tr>
-
</tbody>
-
</table>
-
And here comes the css/js, and after that a simple sample.
JS: filter:js
CSS: filter.css
-
.filter {border-collapse:collapse; table-layout:fixed; width:500px}
-
.filter th {text-align:center; background:#eee; border:1px dotted #777; font:bold 11px verdana; line-height:16px; padding:1px}
-
.filter td {text-indent:5px; overflow:hidden; white-space:nowrap; font:normal 11px verdana}
-
.filter th select {overflow:hidden; font:normal 11px verdana}
-
-
.even td {background:#fff}
-
.odd td {background:#eee}
-
-
.filter table {width:480px; table-layout:fixed; border-collapse:collapse}
-
.filter .holder {width:499px; overflow:auto;}
-
.filter .scrollBar {border:0; background:transparent; width:13px;}
-
* html .filter .scrollBar {width:16px}
-
Sample: filter.html
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
-
-
<html>
-
<head>
-
<title>Filter</title>
-
</head>
-
-
<script src="filter.js"></script>
-
<link rel="stylesheet" href="filter.css" type="text/css">
-
-
<script>
-
window.onload = function() {
-
new createFilter("tabelaUm",10)
-
}
-
</script>
-
-
<body>
-
-
<h2>Filter</h2>
-
-
<script>
-
-
arrayNomes = ['Ricardo','Paulo','Maria','Joana','Rita','Fabiano']
-
arrayNumeros = ['1','2','3','4','5','6','7','8','9']
-
arrayTipo = ['Excellent','OK','Bad']
-
-
tabela = document.createElement("table")
-
tabela.id = "tabelaUm"
-
-
thead = document.createElement("thead")
-
tabela.appendChild(thead)
-
-
tr = document.createElement("tr")
-
-
for (x=0; x<4; x++) {
-
th = document.createElement("th")
-
th.innerHTML = "Header " + (x+1)
-
tr.appendChild(th)
-
}
-
-
thead.appendChild(tr)
-
-
tbody = document.createElement("tbody")
-
-
for (x=0; x<250; x++) {
-
tr = document.createElement("tr")
-
-
td = document.createElement("td")
-
td.innerHTML = arrayNomes[Math.floor(Math.random()*6)]
-
tr.appendChild(td)
-
-
td = document.createElement("td")
-
td.innerHTML = arrayNumeros[Math.floor(Math.random()*9)]
-
tr.appendChild(td)
-
-
td = document.createElement("td")
-
td.innerHTML = arrayNumeros[Math.floor(Math.random()*9)]
-
tr.appendChild(td)
-
-
td = document.createElement("td")
-
td.innerHTML = arrayTipo[Math.floor(Math.random()*3)]
-
tr.appendChild(td)
-
-
tbody.appendChild(tr)
-
}
-
-
tabela.appendChild(tbody)
-
document.getElementsByTagName("body")[0].appendChild(tabela)
-
-
</script>
-
-
</body>
-
</html>
-
Thats basically it. Feel free to use the script anywhere.
If you find something needing some fix, send me an email.
Good luck!