Use value from select2:opening call as input to ajax autocomplete data parameter

tw1742

I'm generating a table of results dynamically and one cell in each row contains an autocomplete. I'm using Select2 to display the autocomplete options and am populating the drop-down from ajax data and custom data unique for each row. My ajax data is coming from my Python app method and I'm using POST to send a variable back to the Python method to use to generate the custom data. Here's how this is set-up:

$(".js-example-tokenizer-ajax-data").select2({
            tags: true,
            createTag: function (params) {
                return {
                  id: params.term,
                  text: params.term,
                  newOption: true
                }
            },
            templateResult: function (data) {
                var $result = $("<span></span>");
                $result.text(data.text);
                if (data.newOption) {
                    $result.append(" <em>(new)</em>");
                }
                return $result;
            },
            placeholder: 'Select an option',
            maximumSelectionLength: 3,
            ajax: {
                url: '{{ url_for("select2Autocomplete") }}',
                contentType: 'application/json',
                method: "POST",
                dataType : 'json',
                data: JSON.stringify({variable:myValue}),
                processResults: function (data) {
                    return {
                        results: data.resource_list
                    };
                }
            }
        });

I've set the id of the select element to a value from my Jinja template.

<select onfocus="this.selectedIndex = -1;" id="{{ key }}" class="js-example-tokenizer-ajax-data" multiple="multiple" miriamId="{{ value }}" style="width:100%">
    <option value="{{ key }}" selected>{{ miriam_name_dict[value] }}</option>
</select>

Since it's dynamic I don't know what that id is for each select. I am able to get the value like this:

$('.js-example-tokenizer-ajax-data').on('select2:opening', function (evt) {
            myValue = document.getElementById(this.id).getAttribute('id');
            console.log("myValue: ", myValue)
    });

I'd like to use myValue as the variable I send back to my python method via the data variable. Is there some way to access myValue to use in the autocomplete code?

tw1742

I was able to pass back data to the autocomplete method for the select element the user clicked on as follows:

$(".js-example-tokenizer-ajax-data").select2({
            tags: true, //Allows for free text input of tag values
            createTag: function (params) {
                return {
                  id: params.term,
                  text: params.term,
                  newOption: true
                }
            },
            templateResult: function (data) {
                var $result = $("<span></span>");
                $result.text(data.text);
                if (data.newOption) {
                    $result.append(" <em>(new)</em>");
                }
                return $result;
            },
            placeholder: 'Select an option',
            maximumSelectionLength: 3,
            ajax: {
                url: '{{ url_for("select2Autocomplete") }}',
                contentType: 'application/json',
                method: "POST",
                dataType : 'json',
                data: function() {
                    var myValue = $(this).attr('id');
                    return JSON.stringify({variable: myValue})
                },
                processResults: function (data) {
                    return {
                        results: data.resource_list
                    };
                }
            }
        });

I was not able to use {variable: $(this).attr('id')} directly as the value for the data parameter and did need to use JSON.stringify on the value to get the value in the python method using jsonData = request.json["variable"]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Setting the value of a select2 input with jquery

From Dev

autocomplete ajax - update input with a specified value

From Dev

Getting selected data in Form from Select2 ajax call

From Dev

Nested Select2 with dependent AJAX call

From Dev

How to capture data parameter from Ajax call in Restful webservice?

From Dev

Select2 use a dynamic Ajax URL on call

From Dev

jquery select2 setting input value

From Dev

Use returned data from Ajax call, dynamic content

From Dev

Select2 - Pass back additional data via ajax call

From Dev

Use data from ajax call without using async: false

From Dev

ASP.NET Core MVC controller receives null for input parameter from ajax call

From Dev

Use a parameter to select data from SQlite database

From Dev

yii select2 AJAX error - data.call not a function

From Dev

autocomplete ajax - update input with a specified value

From Dev

Select2 not loading data into after ajax call

From Dev

Use value from parameter

From Dev

Passing variable value from one function to another in autocomplete with AJAX call

From Dev

How to capture data parameter from Ajax call in Restful webservice?

From Dev

Use select2 ajax data to update hidden inputs

From Dev

Get data value from php array with autocomplete jquery/ajax

From Dev

How to get data attribute of <select> for use in select2 from inside .select2() ajax call?

From Dev

Use returned data from Ajax call, dynamic content

From Dev

Use value from Input Port in Parameter of block - Simulink

From Dev

Use data from ajax call without using async: false

From Dev

ajax jquery autocomplete get data from asmx

From Dev

Select2 JS not showing data from AJAX

From Dev

Select2 Ajax Selected Data Value

From Dev

Kartik Select2 load ajax when other input change value

From Dev

Controller ActionResult methods parameter is not receiving form data from an ajax call

Related Related

  1. 1

    Setting the value of a select2 input with jquery

  2. 2

    autocomplete ajax - update input with a specified value

  3. 3

    Getting selected data in Form from Select2 ajax call

  4. 4

    Nested Select2 with dependent AJAX call

  5. 5

    How to capture data parameter from Ajax call in Restful webservice?

  6. 6

    Select2 use a dynamic Ajax URL on call

  7. 7

    jquery select2 setting input value

  8. 8

    Use returned data from Ajax call, dynamic content

  9. 9

    Select2 - Pass back additional data via ajax call

  10. 10

    Use data from ajax call without using async: false

  11. 11

    ASP.NET Core MVC controller receives null for input parameter from ajax call

  12. 12

    Use a parameter to select data from SQlite database

  13. 13

    yii select2 AJAX error - data.call not a function

  14. 14

    autocomplete ajax - update input with a specified value

  15. 15

    Select2 not loading data into after ajax call

  16. 16

    Use value from parameter

  17. 17

    Passing variable value from one function to another in autocomplete with AJAX call

  18. 18

    How to capture data parameter from Ajax call in Restful webservice?

  19. 19

    Use select2 ajax data to update hidden inputs

  20. 20

    Get data value from php array with autocomplete jquery/ajax

  21. 21

    How to get data attribute of <select> for use in select2 from inside .select2() ajax call?

  22. 22

    Use returned data from Ajax call, dynamic content

  23. 23

    Use value from Input Port in Parameter of block - Simulink

  24. 24

    Use data from ajax call without using async: false

  25. 25

    ajax jquery autocomplete get data from asmx

  26. 26

    Select2 JS not showing data from AJAX

  27. 27

    Select2 Ajax Selected Data Value

  28. 28

    Kartik Select2 load ajax when other input change value

  29. 29

    Controller ActionResult methods parameter is not receiving form data from an ajax call

HotTag

Archive