Sin descripción

autocomplete.js 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. app_label: $element.data('app-label'),
  12. model_name: $element.data('model-name'),
  13. field_name: $element.data('field-name')
  14. };
  15. }
  16. }
  17. }, options);
  18. $element.select2(settings);
  19. };
  20. $.fn.djangoAdminSelect2 = function(options) {
  21. const settings = $.extend({}, options);
  22. $.each(this, function(i, element) {
  23. const $element = $(element);
  24. init($element, settings);
  25. });
  26. return this;
  27. };
  28. $(function() {
  29. // Initialize all autocomplete widgets except the one in the template
  30. // form used when a new formset is added.
  31. $('.admin-autocomplete').not('[name*=__prefix__]').djangoAdminSelect2();
  32. });
  33. $(document).on('formset:added', (function() {
  34. return function(event, $newFormset) {
  35. return $newFormset.find('.admin-autocomplete').djangoAdminSelect2();
  36. };
  37. })(this));
  38. }