-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathautocomplete.js
More file actions
39 lines (35 loc) · 1.22 KB
/
autocomplete.js
File metadata and controls
39 lines (35 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Custom override of Django's autocomplete.js to add the "is_filter" parameter
// to AJAX requests. This parameter enables the backend autocomplete view to
// determine if the request is for filtering, allowing it to include the shared
// option when appropriate (e.g., for filtering shared objects in the admin).
"use strict";
{
const $ = django.jQuery;
$.fn.djangoAdminSelect2 = function () {
$.each(this, function (i, element) {
$(element).select2({
ajax: {
data: (params) => {
return {
term: params.term,
page: params.page,
app_label: element.dataset.appLabel,
model_name: element.dataset.modelName,
field_name: element.dataset.fieldName,
is_filter: element.dataset.isFilter,
};
},
},
});
});
return this;
};
$(function () {
// Initialize all autocomplete widgets except the one in the template
// form used when a new formset is added.
$(".admin-autocomplete").not("[name*=__prefix__]").djangoAdminSelect2();
});
document.addEventListener("formset:added", (event) => {
$(event.target).find(".admin-autocomplete").djangoAdminSelect2();
});
}