Added filter functionality to list II

This commit is contained in:
Simon Martens
2023-02-02 22:27:56 +01:00
parent 490311d73e
commit 112bdc8f51

View File

@@ -77,9 +77,9 @@
}); });
} }
function findWord(word){ function findWord(word, d){
var sw = word.trim().toLowerCase(); var sw = word.trim().toLowerCase();
return dictionary.filter(function(e){ return d.filter(function(e){
if (e.searchitem.indexOf(sw) !== -1) { if (e.searchitem.indexOf(sw) !== -1) {
return true; return true;
} else { } else {
@@ -91,17 +91,22 @@
$(document).ready(function( $ ) { $(document).ready(function( $ ) {
if (document.getElementById("list")) { if (document.getElementById("list")) {
let found = [];
createIndex("list"); createIndex("list");
let found = dictionary;
let sw = "";
let swl = 0;
$("input[name='keyword']").keyup(function() { $("input[name='keyword']").keyup(function() {
var term = $(this).val() || ''; var term = $(this).val() || '';
if( term ) { if( term ) {
for (let item of found) { for (let item of found) {
$(item.element).unmark(); $(item.element).unmark().hide();
} }
$("#list .category").hide(); $("#list .category").hide();
$("#list .searchable").hide(); if (term.length > swl && term.startsWith(sw)) {
found = findWord( term ); found = findWord( term, found);
} else {
found = findWord( term, dictionary );
}
for (let item of found) { for (let item of found) {
$(item.category).addClass("search-expanded").show(); $(item.category).addClass("search-expanded").show();
$(item.element).show(); $(item.element).show();
@@ -109,9 +114,14 @@
$(item.element).mark(term); $(item.element).mark(term);
} }
} }
sw = term;
swl = term.length;
} else { } else {
$("#list .category").show().removeClass("search-expanded").unmark(); $("#list .category").show().removeClass("search-expanded").unmark();
$("#list .searchable").show(); $("#list .searchable").show();
found = dictionary;
sw = "";
swl = 0;
} }
}); });
} }