autocomplete.js 1.1 KB

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