Merge branch 'unified_search' into 'beta'

Unified search

See merge request bde/nk20!95
This commit is contained in:
erdnaxe 2020-08-22 09:38:03 +02:00
commit f077a5d72f
3 changed files with 102 additions and 118 deletions

View File

@ -1,59 +1,16 @@
{% extends "base.html" %}
{% load render_table from django_tables2 %}
{% load i18n %}
{% extends "base_search.html" %}
{% comment %}
SPDX-License-Identifier: GPL-3.0-or-later
{% endcomment %}
{% load i18n perms %}
{% block content %}
<div class="row justify-content-center mb-4">
<div class="col-md-10 text-center">
<input class="form-control mx-auto w-25" type="text" id="search_field"/>
<hr>
{% if can_add_club %}
<a class="btn btn-primary text-center my-4" href="{% url 'member:club_create' %}" data-turbolinks="false">{% trans "Create club" %}</a>
{% endif %}
</div>
</div>
<div class="row justify-content-center">
<div class="col-md-10">
<div class="card card-border shadow">
<div class="card-header text-center">
<h5> {% trans "Club listing" %}</h5>
</div>
<div class="card-body px-0 py-0" id="club_table">
{% render_table table %}
</div>
</div>
</div>
</div>
{% if can_add_club %}
<a class="btn btn-block btn-success mb-3" href="{% url 'member:club_create' %}" data-turbolinks="false">
{% trans "Create club" %}
</a>
{% endif %}
{% endblock %}
{% block extrajavascript %}
<script type="text/javascript">
$(document).ready(function() {
let old_pattern = null;
let searchbar_obj = $("#search_field");
var timer_on = false;
var timer;
function reloadTable() {
let pattern = searchbar_obj.val();
$("#club_table").load(location.pathname + "?search=" + pattern.replace(" ", "%20") + " #club_table", init);
}
searchbar_obj.keyup(function() {
if (timer_on)
clearTimeout(timer);
timer_on = true;
setTimeout(reloadTable, 0);
});
function init() {
$(".table-row").click(function() {
window.document.location = $(this).data("href");
timer_on = false;
});
}
init();
});
</script>
{# Search panel #}
{{ block.super }}
{% endblock %}

View File

@ -1,10 +1,8 @@
{% extends "base.html" %}
{% extends "base_search.html" %}
{% comment %}
SPDX-License-Identifier: GPL-3.0-or-later
{% endcomment %}
{% load render_table from django_tables2 %}
{% load i18n crispy_forms_tags perms %}
{% block contenttitle %}{% endblock %}
{% load i18n perms %}
{% block content %}
{% if "member.change_profile_registration_valid"|has_perm:user %}
@ -13,63 +11,6 @@ SPDX-License-Identifier: GPL-3.0-or-later
</a>
{% endif %}
<div class="card bg-light">
<h3 class="card-header text-center">
{{ title }}
</h3>
<div class="card-body">
<input id="searchbar" type="text" class="form-control" placeholder="Nom/prénom/note/section...">
</div>
<div class="card-body">
<div id="user_table">
{% if table.data %}
{% render_table table %}
{% else %}
<div class="alert alert-warning">
{% trans "There is no user with this pattern." %}
</div>
{% endif %}
</div>
</div>
</div>
{# Search panel #}
{{ block.super }}
{% endblock %}
{% block extrajavascript %}
<script type="text/javascript">
let pattern = '';
function reloadTable() {
pattern = $("#searchbar").val();
if (pattern.length > 2)
$("#user_table").load(location.pathname + "?search=" + pattern.replace(" ", "%20") + " #user_table", init_table);
}
function init_table() {
// On row click, go to object
$(".table-row").click(function () {
window.document.location = $(this).data("href");
});
// Highlight searched terms
$("tr").each(function () {
$(this).find("td:eq(0), td:eq(1), td:eq(2), td:eq(3), td:eq(5)").each(function () {
$(this).html($(this).text().replace(new RegExp(pattern, 'i'), "<mark>$&</mark>"));
});
});
}
$(document).ready(function () {
// Recover last search from url
let searchParams = new URLSearchParams(window.location.search)
if (searchParams.has('search'))
pattern = searchParams.get('search');
// On search, refresh table
$("#searchbar").keyup(debounce(reloadTable, 300));
// First init
init_table();
});
</script>
{% endblock %}

View File

@ -0,0 +1,86 @@
{% extends "base.html" %}
{% comment %}
SPDX-License-Identifier: GPL-3.0-or-later
{% endcomment %}
{% load render_table from django_tables2 %}
{% load i18n perms %}
{% block contenttitle %}{% endblock %}
{% block content %}
<div class="card bg-light">
<h3 class="card-header text-center">
{{ title }}
</h3>
<div class="card-body">
<input id="searchbar" type="text" class="form-control"
placeholder="{% trans "Search by attribute such as name…" %}">
</div>
<div id="dynamic-table">
{% if table.data %}
{% render_table table %}
{% else %}
<div class="card-body">
<div class="alert alert-warning">
{% trans "There is no results." %}
</div>
</div>
{% endif %}
</div>
</div>
{% endblock %}
{% block extrajavascript %}
<script type="text/javascript">
function reloadTable() {
pattern = $("#searchbar").val();
if (pattern.length > 2)
$("#dynamic-table").load(location.pathname + "?search=" + pattern.replace(" ", "%20") + " #dynamic-table", init_table);
}
function init_table() {
// On row click, go to object
$(".table-row").click(function () {
window.document.location = $(this).data("href");
});
// Highlight searched terms
const pattern = $("#searchbar").val();
if (pattern.length > 2) {
$("tr").each(function () {
$(this).find("td:eq(0), td:eq(1), td:eq(2), td:eq(3), td:eq(5)").each(function () {
$(this).html($(this).text().replace(new RegExp(pattern, 'i'), "<mark>$&</mark>"));
});
});
}
}
$(document).ready(function () {
let pattern = '';
// Recover last search from url
let searchParams = new URLSearchParams(window.location.search)
if (searchParams.has('search')) {
pattern = searchParams.get('search');
}
// On search, refresh table
$("#searchbar").keyup(debounce(function () {
// Get new pattern
pattern = $("#searchbar").val();
// Set URL
searchParams.set('search', pattern);
history.pushState({}, null, "?" + searchParams.toString());
// If long enough, update table
if (pattern.length > 2) {
$("#dynamic-table").load(location.pathname + "?search=" + pattern.replace(" ", "%20") + " #dynamic-table", init_table);
}
}, 300));
// First init
init_table();
});
</script>
{% endblock %}