autocomplete.js 1.11 KB
Newer Older
Administrator committed
1 2 3 4 5 6 7 8 9 10 11 12 13
(function($) {
    'use strict';
    var init = function($element, options) {
        var settings = $.extend({
            ajax: {
                data: function(params) {
                    return {
                        term: params.term,
                        page: params.page
                    };
                }
            }
        }, options);
14

Administrator committed
15 16 17 18 19
        $element.select2(settings);
    };

    $.fn.djangoAdminSelect2 = function(options) {
        var settings = $.extend({}, options);
20

Administrator committed
21 22
        $.each(this, function(i, element) {
            var $element = $(element);
23

Administrator committed
24 25
            init($element, settings);
        });
26

Administrator committed
27 28 29 30 31 32
        return this;
    };

    $(function() {
        // Initialize all autocomplete widgets except the one in the template
        // form used when a new formset is added.
33 34
        $('.admin-autocomplete').not('[name*=__prefix__]')
            .djangoAdminSelect2();
Administrator committed
35 36 37 38 39 40 41 42
    });

    $(document).on('formset:added', (function() {
        return function(event, $newFormset) {
            return $newFormset.find('.admin-autocomplete').djangoAdminSelect2();
        };
    })(this));
}(django.jQuery));