#32628: Add extra data to autocomplete request
-------------------------------+--------------------------------------
     Reporter:  Seb G          |                    Owner:  nobody
         Type:  New feature    |                   Status:  closed
    Component:  contrib.admin  |                  Version:  3.2
     Severity:  Normal         |               Resolution:  wontfix
     Keywords:                 |             Triage Stage:  Unreviewed
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+--------------------------------------

Comment (by Seb G):

 Dear Johannes,

 I found the following code to both add the requested feature (being able
 to add extra GET params to the AJAX request) **and** preserve Select2 API.

 I also took the liberty of passing the `true` parameter to the `$.extend`
 call that merges default Django settings with user-defined settings in the
 `init` function (line 11), since omitting it caused a bug where other
 parameters (passed through `options`) nested under the `ajax` key were
 overwritten to `undefined` (see relevant documentation here:
 https://api.jquery.com/jQuery.extend/#jQuery-extend-deep-target-
 object1-objectN).

 - `autocomplete.js`:

 {{{#!javascript
 'use strict';
 {
     const $ = django.jQuery;
     const init = function($element, options) {
         let userData = function (params) {};
         if (options.ajax && options.ajax.data) {
             userData = options.ajax.data;
             delete options.ajax.data;
         }

         const settings = $.extend(true, {
             ajax: {
                 data: function(params) {
                     const defaultData = {
                         term: params.term,
                         page: params.page,
                         app_label: $element.data('app-label'),
                         model_name: $element.data('model-name'),
                         field_name: $element.data('field-name')
                     };
                     return $.extend(defaultData, userData(params))
                 }
             }
         }, 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));
 }
 }}}

 - `consumer_script.js`:

 {{{#!javascript
 $select = $("#id_my_field");
 $select.select2("destroy").djangoAdminSelect2({
     ajax: {
         data: function (params) {
             return {foo: "bar"}
         }
         // Adding other AJAX settings is now also possible, such as
 processResults for example
         processResults: function (data) {
             console.log("Now possible")
             return data
         }
     }
 });
 }}}

 Do you think this approach would be compatible with your flow?

 Regards

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32628#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.d6853cd1e4fde9713bf65dbf940dedd1%40djangoproject.com.

Reply via email to