【发布时间】:2013-10-29 12:13:22
【问题描述】:
我使用 php 从 .csv 生成一个 html 表。
我得到了这个运行良好的 JavaScript Livesearch,我用它来搜索表格。
这是代码:
function doSearch() {
var searchText = document.getElementById('searchTerm').value;
var targetTable = document.getElementById('dataTable');
var targetTableColCount;
//Loop through table rows
for (var rowIndex = 0; rowIndex < targetTable.rows.length; rowIndex++) {
var rowData = '';
//Get column count from header row
if (rowIndex == 0) {
targetTableColCount = targetTable.rows.item(rowIndex).cells.length;
continue; //do not execute further code for header row.
}
//Process data rows. (rowIndex >= 1)
for (var colIndex = 0; colIndex < targetTableColCount; colIndex++) {
rowData += targetTable.rows.item(rowIndex).cells.item(colIndex).textContent;
}
//If search term is not found in row data
//then hide the row, else show
if (rowData.indexOf(searchText) == -1)
targetTable.rows.item(rowIndex).style.display = 'none';
else
targetTable.rows.item(rowIndex).style.display = 'table-row';
}
function capitaliseFirstLetter(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
}
搜索对大写字母区分大小写,我希望它忽略这一点,以便它无论如何都会找到条目。
如果我搜索“jon doe”,我想找到:“jon doe”、“Jon doe”、“Jon Doe”、“JON DOE”等。
如何在我现有的代码中实现这一点?
【问题讨论】:
-
您是否尝试过将大写与大写进行比较,例如当您找到要查找的字符串时,例如 Jon Doe,将其转换为 JON DOE,然后与您要比较的字符串进行比较, 也将其转换为 JON DOE,这样无论表中的内容或您要搜索的内容,您都只会比较所有大写字母。
-
这对我来说似乎是个好主意,不过我对 JS 很陌生,你能告诉我你将如何在代码中做到这一点吗?
标签: javascript search html-table livesearch