Select2 Ajax Method Not Selecting

后端 未结 6 719
故里飘歌
故里飘歌 2020-12-04 13:46

Ok, I\'m sure there\'s something simple set wrong here but I\'m not 100% what it is.

So I am trying to use Select2 AJAX method as a way of users to search a database

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 14:42

    Like Dropped.on.Caprica said: Every result item needs an unique id.

    So just assign every result id an unique number:

    $('#searchDriver').select2({
        ajax: {
           dataType: 'json',
           delay: 250,
           cache: true,
           url: '/users/search',
           processResults: function (data, params) {
               //data is an array of results
               data.forEach(function (d, i) {
                   //Just assign a unique number or your own unique id
                   d.id = i; // or e.id = e.userId;
               })
    
               return {
                   results: data
               };
           },
        },
        escapeMarkup: function (markup) { return markup; },
        minimumInputLength: 1,
        templateResult: formatRepo,
        templateSelection: formatRepoSelection
    });
    

提交回复
热议问题