input.Select2 does not let you select the record

Asked

Viewed 78 times

0

In the <select> the records coming from the database are listed, but when clicking on the record, it is not selected.

function searchClasseDisciplina() {
  const input = $('#form-classe').find('#disciplina');
  const token = sessionStorage.getItem('token');
  input.select2({
    placeholder: 'Disciplina',
    allowClear: true,
    minimumInputLength: 1,
    language: {
      errorLoading: function () {
      return 'registros não encontrados';
    },
    searching: function () {
      return null;
    },
    inputTooShort: function () {
      return 'digite 1 ou mais caracteres';
    }
  },
  ajax: {
    url: function (params) {
      return getApi() + '/dis/'+params.term;
    },
    dataType: 'json',
    headers: { 'x-access-token': token },
    processResults: function (data) {        
      return {
        results: data.results.map(x => ({
          dis_id: x.dis_id,
          text: $( /*html*/ `
            <span>${x.dis_nome}</span>
            <small class="pull-right">Cód.: <b>${x.dis_codigo}</b></small>
          `)
        }))
      };        
    },     
  },
}).on("select2:select", function () {
  $.ajax({
    type: 'get',
    url: getApi() + `/dis/${input.val()}`,
    cache: false,
    contentType: "application/json;charset=utf-8",
    headers: { 'x-access-token': token },
    success: function (response) {
      addDisciplinaToClasse(response);
    },
    error: function (error) {
      showError(error);
    },
  });
});
openSelect(input);

}

inserir a descrição da imagem aqui

1 answer

0


I believe the problem lies in the code snippet where you are processing the results:

processResults: function (data) {        
      return {

        results: data.results.map(x => ({

          dis_id: x.dis_id,//<--------- AQUI --------->

          text: $( /*html*/ `
            <span>${x.dis_nome}</span>
            <small class="pull-right">Cód.: <b>${x.dis_codigo}</b></small>
          `)
        }))
      };        

According to this link, as chaves sane id (instead of dis_id) and text

Browser other questions tagged

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