1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-06-21 01:48:21 +02:00

Added a search tab for the conso page

This commit is contained in:
Nicolas Margulies
2023-10-25 20:01:48 +02:00
parent ff6e207512
commit 0cc130092f
5 changed files with 153 additions and 62 deletions

View File

@ -103,6 +103,11 @@ SPDX-License-Identifier: GPL-3.0-or-later
</a>
</li>
{% endfor %}
<li class="nav-item">
<a class="nav-link font-weight-bold" data-toggle="tab" href="#search">
{% trans "Search" %}
</a>
</li>
</ul>
</div>
@ -123,6 +128,20 @@ SPDX-License-Identifier: GPL-3.0-or-later
</div>
</div>
{% endfor %}
<div class="tab-pane" id="search">
<input class="form-control mx-auto d-block mb-3"
placeholder="{% trans "Search button..." %}" type="text" id="search-input"/>
<div class="d-inline-flex flex-wrap justify-content-center" id="search-results">
{% for button in search_results %}
{% if button.display %}
<button class="btn btn-outline-dark rounded-0 flex-fill"
id="search_button{{ button.id }}" name="button" value="{{ button.name }}">
{{ button.name }} ({{ button.amount | pretty_money }})
</button>
{% endif %}
{% endfor %}
</div>
</div>
</div>
</div>
@ -182,5 +201,37 @@ SPDX-License-Identifier: GPL-3.0-or-later
{% endif %}
{% endfor %}
{% endfor %}
{% for button in search_results %}
{% if button.display %}
$("#search_button{{ button.id }}").click(function() {
addConso({{ button.destination_id }}, {{ button.amount }},
{{ polymorphic_ctype }}, {{ button.category_id }}, "{{ button.category.name|escapejs }}",
{{ button.id }}, "{{ button.name|escapejs }}");
});
{% endif %}
{% endfor %}
searchbar = document.getElementById("search-input")
const parser = new DOMParser();
old_pattern = null;
function updateSearch(force = false) {
let pattern = searchbar.value
if ((pattern === old_pattern || pattern === "") && !force)
return;
const xhr = new XMLHttpRequest();
xhr.open("GET", location.pathname + "?search=" +
encodeURI(pattern) + "#search", true)
xhr.onload = () => {
document.getElementById("search-results").innerHTML =
parser.parseFromString(xhr.responseText, "text/html").getElementById("search-results").innerHTML
};
xhr.send();
}
searchbar.addEventListener("input", function (e) {
debounce(updateSearch)()
});
</script>
{% endblock %}