本帖最后由 yanjun202 于 2026-7-1 16:32 编辑
搜索部分优化 参考一下:1157行 doSearch 函数
[JavaScript] 纯文本查看 复制代码
function doSearch() {
var query = searchInput.value.trim().toUpperCase();
if (!query) {
currentData = allData.slice();
renderTable(currentData);
if (selectedId !== null) {
var stillExists = false;
for (var i = 0; i < allData.length; i++) {
if (allData[i].id === selectedId) {
stillExists = true;
break;
}
}
if (!stillExists) {
selectedId = null;
detailBox.innerHTML = '<div class="empty-hint">👈 点击左侧列表中的单词查看详细释义</div>';
} else {
selectWord(selectedId);
}
} else {
detailBox.innerHTML = '<div class="empty-hint">👈 点击左侧列表中的单词查看详细释义</div>';
}
return;
}
var titleMatches = [];
var otherMatches = [];
for (var i = 0; i < allData.length; i++) {
var w = allData[i];
var match = false;
var isTitleMatch = false;
if (w.word.toUpperCase().indexOf(query) !== -1) {
match = true;
isTitleMatch = true;
} else if (w.meaning.toUpperCase().indexOf(query) !== -1) match = true;
else if (w.translation.toUpperCase().indexOf(query) !== -1) match = true;
else if (w.category.toUpperCase().indexOf(query) !== -1) match = true;
else if (w.extension && w.extension.toUpperCase().indexOf(query) !== -1) match = true;
else {
for (var j = 0; j < w.examples.length; j++) {
var ex = w.examples[j];
if (ex.db.toUpperCase().indexOf(query) !== -1 || ex.sql.toUpperCase().indexOf(query) !== -1) {
match = true;
break;
}
}
}
if (match) {
if (isTitleMatch) titleMatches.push(w);
else otherMatches.push(w);
}
}
currentData = titleMatches.concat(otherMatches);
renderTable(currentData);
if (selectedId !== null) {
var stillInResult = false;
for (var i = 0; i < filtered.length; i++) {
if (filtered[i].id === selectedId) {
stillInResult = true;
break;
}
}
if (!stillInResult) {
selectedId = null;
detailBox.innerHTML = '<div class="empty-hint">👈 点击左侧列表中的单词查看详细释义</div>';
} else {
selectWord(selectedId);
}
} else {
detailBox.innerHTML = '<div class="empty-hint">👈 点击左侧列表中的单词查看详细释义</div>';
}
}
|