autocomplete.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. {
  3. const $ = django.jQuery;
  4. const init = function($element, options) {
  5. const settings = $.extend({
  6. ajax: {
  7. data: function(params) {
  8. return {
  9. term: params.term,
  10. page: params.page
  11. };
  12. }
  13. }
  14. }, options);
  15. $element.select2(settings);
  16. };
  17. $.fn.djangoAdminSelect2 = function(options) {
  18. const settings = $.extend({}, options);
  19. $.each(this, function(i, element) {
  20. const $element = $(element);
  21. init($element, settings);
  22. });
  23. return this;
  24. };
  25. $(function() {
  26. // Initialize all autocomplete widgets except the one in the template
  27. // form used when a new formset is added.
  28. $('.admin-autocomplete').not('[name*=__prefix__]').djangoAdminSelect2();
  29. });
  30. $(document).on('formset:added', (function() {
  31. return function(event, $newFormset) {
  32. return $newFormset.find('.admin-autocomplete').djangoAdminSelect2();
  33. };
  34. })(this));
  35. }