Saturday, February 19, 2022

[FIXED] select2 js empty value on load in select option

Issue

I have implemented select2js in my project, In few cases when i load my page, an empty value is appending to my select input box. Can anyone help me out, what is the issue, I have added a image of it and My code is

{!! Form::select('searchSkillTools[]',[],(isset($skillToolXref) && $skillToolXref!='')?$skillToolXref:'', array('autocomplete' => 'off', 'class' => 'chosen-select','multiple'=>'multiple', 'id' => 'searchSkillTools-'.$randomId, 'placeholder' => '' ,'style' => 'height: auto !important')) !!}

$('#searchSkillTools-{{$randomId}}').select2({  
    ajax: {
        url: skillToolUrl,
        dataType: 'json',
        method:'get',
        delay: 250,//delay in response
        data: function (data) {
            return {
                search: data.term //search term
            };
        },
        /*option to transform the data returned by response into the format expected by Select2*/
        processResults: function (response) {            
            return {
                results:response
            };
        },
        cache: true
    }
});

enter image description here


Solution

This way will fix the bug:

(isset($skillToolXref) && $skillToolXref!='')?$skillToolXref:null



Answered By - Andy

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.