Select2 + Jquery - Autocomplete Problem

Asked

Viewed 393 times

0

I managed to make the Select2 Plugin work, but the autocomplete does not work.

$(function () {

    $("#disciplina").select2({
        placeholder: "Disciplina",
        minimumInputLength: 0,
        ajax:{
            url: "busca.asp",
            type: "post",
            dataType: "json",
            delay: 150,
            data: function(params){
                return{
                    text: params.term
                };
            },
            processResults: function(data, params){
                params.page = params.page || 1;     
                return{ 

                    results: data,
                    pagination:{
                        more: (params.page * 30) < data.total_count
                    }
                };
            },
            cache: true
        }
    });

});

Ao começar a digitar a disciplina BIOLOGIA deveria buscar

Someone’s been through it?

Return with console.log(date);

inserir a descrição da imagem aqui

console.log(params);

inserir a descrição da imagem aqui

  • You can dump the information you are receiving from the server (console.log)

  • @Jorgecosta already tried to put the console in the return of ajax, but the function stops working. Return{ console.log(params.term); text: params.term };

  • Dude, I’m about 3 weeks into this popular select one, it’s really hard. I came close now, but I’m bumping into this.

  • Console.log(data) before Return, if you’re using Chrome see the inspector

  • @Jorgecosta did in processResults and it worked, I will post in the question the return image.

  • I published an answer essentially it is your responsibility to filter the data on the server side when you use ajax

Show 1 more comment

1 answer

1


Problem

Data is not being filtered when a search key is typed.

According to the documentation on the plugin site: https://select2.org/data-sources/ajax

Select2 expects Results from the remote endpoint to be Filtered on the server side.

Translation

Select2 expects results returned by endpoint set remote are filtered from the server side.

Example

In this example date is a function allows you to customize the url of the query, on the server side these parameters should be used to filter the returned list.

Query Parameters will be ? search=[term]&type=public

$('#mySelect2').select2({
  ajax: {
    url: 'https://api.github.com/orgs/select2/repos',
    data: function (params) {
      var query = {
        search: params.term,
        type: 'public'
      }

      // Query parameters will be ?search=[term]&type=public
      return query;
    }
  }
});
  • Thank you very much, but I’m giving up, I have no idea how to do it. Tired already. Thanks for the attention.

  • Please put my answer as right, I will point you to another plugin to view in https://answall.com/questions/316058/bootstrap-3-typeahead-listoptions-com-base-searchdisplaying other

  • I saw it, but it doesn’t answer what I want. It’s an autocomplete only, Select2 is a dynamic select.

  • I believe I am very close to the solution.

  • I do not understand why not marked the answer as correct, it responds correctly to your problem, the plugin I recommended is effectively dynamic, can change the datasource while changing the search key

  • It does not list all options and does not look like a select, but for other situations is very useful, but does not meet me.

  • what I want is this https://www.aprovaconcursos.com.br/questoes-de-concurso/questoes/institutiono/ENEM/filtro/auto

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.