我創建了一個表,我想搜索兩列(代碼和名稱),所以當用戶輸入任何字母或名稱、代碼列時,它會顯示包含該字母的所有行名,我想在搜索框旁邊顯示我的表的總行數。
in HTML
<input type="text" class="form-control" onkeyup="searchrecords" id="searchtext" placeholder="Search" aria-label="Text input with dropdown button">
<h6 class="mt-3 mx-4">Total: </h6>
</div>
</div>
<table class="table table-striped " id="table1">
<thead>
<tr class="feed-bg text-white ">
<th>ID</th>
<th>Employee Code</th>
<th>Employee Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
{% for i in data_list %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{i.employee_code}}</td>
<td>{{i.employee_name}}</td>
<td>{{i.email}}</td>
</tr>
{% endfor %}
</tbody>
</table>
在JS中,我做了搜索代碼,但它不起作用!
function searchrecords()
{
var input,table,tr,td,filter;
input=document.getElementById("searchtext");
filter=input.value.toUpperCase();
table=document.getElementById("table1");
tr=table.getElementsByTagName("tr");
for(i=0;i<tr.length;i++)
{
td=tr[i].getElementsByTagName("td")[0];
if(td)
{
textdata=td.innerText;
if(textdata.toUpperCase().indexOf(filter)>-1)
{
tr[i].style.display="";
}
else
{
tr[i].style.display="none";
}
}
}
}
我在這里為你樹立了一個榜樣。也許會有幫助。