diff options
Diffstat (limited to 'media/admin/js/autocomplete.js')
-rw-r--r-- | media/admin/js/autocomplete.js | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/media/admin/js/autocomplete.js b/media/admin/js/autocomplete.js new file mode 100644 index 0000000..c55eee1 --- /dev/null +++ b/media/admin/js/autocomplete.js @@ -0,0 +1,41 @@ +'use strict'; +{ + const $ = django.jQuery; + const init = function($element, options) { + const settings = $.extend({ + ajax: { + data: function(params) { + return { + term: params.term, + page: params.page, + app_label: $element.data('app-label'), + model_name: $element.data('model-name'), + field_name: $element.data('field-name') + }; + } + } + }, options); + $element.select2(settings); + }; + + $.fn.djangoAdminSelect2 = function(options) { + const settings = $.extend({}, options); + $.each(this, function(i, element) { + const $element = $(element); + init($element, settings); + }); + 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).on('formset:added', (function() { + return function(event, $newFormset) { + return $newFormset.find('.admin-autocomplete').djangoAdminSelect2(); + }; + })(this)); +} |