last update feat quick search

This commit is contained in:
arifal
2025-05-06 15:06:59 +07:00
parent 2618ac06d0
commit becc368069
11 changed files with 507 additions and 249 deletions

View File

@@ -4,13 +4,56 @@ class QuickSearchResult {
constructor() {
this.table = null;
const baseInput = document.getElementById("base_url_datatable");
this.datatableUrl = baseInput ? baseInput.value : "";
this.baseUrl = baseInput ? baseInput.value.split("?")[0] : "";
this.keywordInput = document.getElementById("search_input");
this.searchButton = document.getElementById("search_button");
this.datatableUrl = this.buildUrl(this.keywordInput.value);
}
init() {
this.bindSearchButton();
this.initDatatable();
}
bindSearchButton() {
const handleSearch = () => {
const newKeyword = this.keywordInput.value.trim();
if (newKeyword !== "") {
// 1. Update datatable URL and reload
this.datatableUrl = this.buildUrl(newKeyword);
this.initDatatable();
// 2. Update URL query string (without reloading the page)
const newUrl = `${
window.location.pathname
}?keyword=${encodeURIComponent(newKeyword)}`;
window.history.pushState({ path: newUrl }, "", newUrl);
// 3. Update visible keyword text in <em>{{ $keyword }}</em>>
const keywordDisplay = document.querySelector(".qs-header em");
if (keywordDisplay) {
keywordDisplay.textContent = newKeyword;
}
}
};
this.searchButton.addEventListener("click", handleSearch);
this.keywordInput.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
event.preventDefault();
handleSearch();
}
});
}
buildUrl(keyword) {
const url = new URL(this.baseUrl, window.location.origin);
url.searchParams.set("search", keyword);
return url.toString();
}
initDatatable() {
const tableContainer = document.getElementById(
"datatable-quick-search-result"
@@ -73,7 +116,12 @@ class QuickSearchResult {
};
if (this.table) {
this.table = this.table.updateConfig(config).forceRender();
this.table
.updateConfig({
...config,
server: { ...config.server, url: this.datatableUrl },
})
.forceRender();
} else {
tableContainer.innerHTML = "";
this.table = new Grid(config).render(tableContainer);