Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select2 ajax shows results but can't select

I'm using select2 plugin with remote ajax data. I can see the results in the dropdown but can't select them. I want the results to be selectable and placed into the field after selection. I think the problem is with passing the id, I don't know how to pass it correctly.. Any ideas?

my json for ?tag_word=for ...there is no id

results: [{text: "fort"}, {text: "food"}]

Here the code:

    <select class="js-data-example-ajax" style="width:100%">
      <option selected="selected">asdasd</option>
    </select>

    <link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet" />
    <script type="text/javascript" src="{% static 'js/select2.js' %}"></script>
    <script >
    $(document).ready(function(){
    $('.js-data-example-ajax').select2({
    minimumInputLength: 2,
    multiple:true,
    delay: 250,
    cache: true,
    ajax: {
      url: '/tags/search/autocomplete/',
      dataType: 'json',
      data: function (parms, page) { return { tag_word: parms.term }; },
    },
  });
});  
    </script>

here is the server code:

def autocomplete(request):
s = SearchQuerySet(using='autocomplete')
sqs = s.autocomplete(content_auto=request.GET.get('tag_word'))[:5]
suggestions = [  {'text':result.tag_word,
                    'id':result.tag_word,} for result in sqs]
the_data = json.dumps({
    'results': suggestions
})
return HttpResponse(the_data, content_type='application/json')
like image 902
Torostar Avatar asked Jul 24 '15 17:07

Torostar


2 Answers

To select an option each and every result need to have a unique id. Therefore you can add id using following code(processResults).

$('.js-data-example-ajax').select2({
    minimumInputLength: 2,
    multiple:true,
    delay: 250,
    cache: true,
    ajax: {
        url: '/tags/search/autocomplete/',
        dataType: 'json',
        data: function (parms, page) { return { tag_word: parms.term }; },
        processResults: function (data) {
                    data.results.forEach(function (entry, index) {
                        entry.id = ''+index; // Better if you can assign a unique value for every entry, something like UUID
                    });

                    return data;
                },
                cache: true
    },
});
like image 52
tk_ Avatar answered Nov 06 '22 14:11

tk_


Its because you got all id's null. change to numbers. Click on the get selected button to see the ids only will be passed.

var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];

$(document).ready(function(){

    $('.js-data-example-ajax').select2({
    minimumInputLength: 2,
    multiple:true,
    delay: 250,
    data: data


  });
});

$(".checkoutput").click(function(){
console.log($('.js-data-example-ajax').val());
})

Here is the JSFIDDLE

like image 39
Alaksandar Jesus Gene Avatar answered Nov 06 '22 14:11

Alaksandar Jesus Gene