Search code examples
jqueryajaxjquery-select2jquery-select2-4

select2 display default option (static) and do ajax search if not found


I have select2 v4 with a static select (100 options). When the user input is not found in the default options list, I need to do an ajax search.

As I have not been able to use the same select object, I use a different for the ajax search. So I hide the default select and replace with the ajax_select. Do the ajax search.

code:

<script type="text/javascript">

    var s2_search =''; //global var for search string

    $("#default_select").select2({
        placeholder: "Select an item",
        debug: true,
        minimumInputLength: 0, // important!!! => without setting to 0 the default data will not be shown

        tags:true,
        createTag: function (params) {
            s2_search = params.term; //assing search string to var
        },

        templateResult: function(data){

            var $result = $("<span></span>");

            $result.text(data.text);

            return $result;
        },
        escapeMarkup: function (markup) {
            if(markup == 'No results found'){
               // item is not found in default activate the ajax select
                $('#div_default').addClass('hidden');
                $('#div_new').removeClass('hidden');
                new_select_func(s2_search)
            }
            else {
                return markup;
            }
        }
    });

    var $myselect = $('#new_select').select2({
        minimumInputLength: 0,
        ajax: {
            url: BASE_URL + '/get_more',
            dataType: 'json',
            cache: true,
            delay: 350,
            data: function (params) {
                return {
                    q: params.term//, // search term
                };
            },
            processResults: function (data, params) {
                if(data === false){
                    // if the item is not found in the database display a modal form to insert the item
                    // This works 
                    $.get(BASE_URL+'/get_new_form',{plate: params.term}, function (data) {
                        $('#myModal').find('.modal-body').html(data);
                    });
                    $('#myModal').modal('show');
                }
                else {
                    return {
                        results: data 
                    };
                }
            }
        },
        templateSelection: formatNewSelection 

    });

    function formatNewSelection(data, container) {

        // append the new item to the default select
        $("#default_select").append($('<option>', {value: data.id, text: data.text}));
        //restore default select 
        $('#div_default').removeClass('hidden');
        // hide new select
        $('#div_new').addClass('hidden');

        return data.text;
    }
    function new_select_func(search_val) {
        //close default select 
        $("#default_select").select2("close");
        //open new select 
        $myselect.select2('open');
        // set search string and trigger ajax search
        var $search = $myselect.data('select2').dropdown.$search || $myselect.data('select2').selection.$search;
        $search.val(search_val);
        $search.trigger('keyup');

    }
</script>

Definitely not the best way to do it, but I only have one issue. When I select an item in the ajax_select to append it to default select it create the same option 4 times. Can anyone tell me why and is there a better way to do it?

Thank you, Salain


Solution

  • I have resolve the multiple option appended to default_select when making a selection in ajax_select (new_select). The issue was with the templateSelection function formatNewSelection, I have replaced it with select2:select event.

    Code for new_select :

    var $myselect = $('#new_select').select2({
        minimumInputLength: 0,
        ajax: {
            url: BASE_URL + '/get_more',
            dataType: 'json',
            cache: true,
            delay: 350,
            data: function (params) {
                return {
                    q: params.term//, // search term
                };
            },
            processResults: function (data, params) {
                if(data === false){
                    // if the item is not found in the database display a modal form to insert the item
                    // This works 
                    $.get(BASE_URL+'/get_new_form',{plate: params.term}, function (data) {
                        $('#myModal').find('.modal-body').html(data);
                    });
                    $('#myModal').modal('show');
                }
                else {
                    return {
                        results: data 
                    };
                }
            }
        } 
       // the Selection template and function are not needed using default format
       //,templateSelection: formatNewSelection 
    
    });
    
    $myselect.on("select2:select",  function(e) {
    
        // append the new item to the default select
        $("#default_select").append($('<option>', {value: e.params.data.id, text: e.params.data.text}));
        //restore default select 
        $('#div_default').removeClass('hidden');
        // hide new select
        $('#div_new').addClass('hidden');
    
    });
    

    This is still not the best piece of coding, but it works for now. Any help on improving this would be mostly appreciated.