🎨 Better autocomplete field

This commit is contained in:
Yohann D'ANELLO 2020-08-06 18:27:57 +02:00
parent 5b3361f086
commit d95cd8c7c7
3 changed files with 9 additions and 3 deletions

View File

@ -54,7 +54,7 @@ class UserViewSet(ReadProtectedModelViewSet):
serializer_class = UserSerializer serializer_class = UserSerializer
filter_backends = [DjangoFilterBackend, SearchFilter] filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ['id', 'username', 'first_name', 'last_name', 'email', 'is_superuser', 'is_staff', 'is_active', ] filterset_fields = ['id', 'username', 'first_name', 'last_name', 'email', 'is_superuser', 'is_staff', 'is_active', ]
search_fields = ['$username', '$first_name', '$last_name', ] search_fields = ['$username', '$first_name', '$last_name', '$note__alias__name', '$note__alias__normalized_name', ]
# This ViewSet is the only one that is accessible from all authenticated users! # This ViewSet is the only one that is accessible from all authenticated users!

View File

@ -326,8 +326,8 @@ class ClubListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView):
qs = qs.filter( qs = qs.filter(
Q(name__iregex=pattern) Q(name__iregex=pattern)
| Q(note__alias__name__iregex="^" + pattern) | Q(note__alias__name__iregex=pattern)
| Q(note__alias__normalized_name__iregex=Alias.normalize("^" + pattern)) | Q(note__alias__normalized_name__iregex=Alias.normalize(pattern))
) )
return qs return qs

View File

@ -10,6 +10,8 @@ $(document).ready(function () {
if (!name_field) if (!name_field)
name_field = "name"; name_field = "name";
let input = target.val(); let input = target.val();
target.addClass("is-invalid");
target.removeClass("is-valid");
$("#" + prefix + "_reset").removeClass("d-none"); $("#" + prefix + "_reset").removeClass("d-none");
$.getJSON(api_url + (api_url.includes("?") ? "&" : "?") + "format=json&search=^" + input + api_url_suffix, function(objects) { $.getJSON(api_url + (api_url.includes("?") ? "&" : "?") + "format=json&search=^" + input + api_url_suffix, function(objects) {
@ -27,6 +29,10 @@ $(document).ready(function () {
target.val(obj[name_field]); target.val(obj[name_field]);
$("#" + prefix + "_pk").val(obj.id); $("#" + prefix + "_pk").val(obj.id);
results_list.html("");
target.removeClass("is-invalid");
target.addClass("is-valid");
if (typeof autocompleted != 'undefined') if (typeof autocompleted != 'undefined')
autocompleted(obj, prefix) autocompleted(obj, prefix)
}); });