Dynamic completion autocomplete

Asked

Viewed 166 times

1

I’m trying to use the Bootstrap + Chosen, in my select option to make a combobox with autocomplete.

Only I’m filling in the select dynamically with jquery.

But when implementing Bootstrap + Chosen the dynamic padding doesn’t work, while Chosen does and if I take out Chosen the dynamic padding works.

My fill is like this:

function carregarComboSetor( idSetor ){
      $.ajax({
          url      : 'funcao/setor.php',
          type     : 'post',
          dataType : 'json',
          data : {
            acao : 'S',
            cdsetor : idSetor
          },
          success : function (data) {
              var op = "<option value='0'>Selecione um setor</option>";
              $('#setor').append(op);
              $.each( data.setor, function (key, value) {
                  var option = "";
                  if( idSetor === value.codsetor ){
                      option = "<option value='"+ value.codsetor +"' selected>"
                                 + value.nmsetor
                               +"</option>";

                  }else{
                      option = "<option value='"+ value.codsetor +"'>"
                               + value.nmsetor
                               +"</option> ";
                  }
                 $('#setor').append(option);
              } );
              $('#setor').val( idSetor );
          }

      });

}

EDITION 1

In the console only shows these texts

inserir a descrição da imagem aqui

  • See if any are returning erro in the log console.

  • Do not return errors, in fact only those notices as per editing

2 answers

1


I managed to solve!

I added within my function to load Combo the following line:

$('#setor').trigger("chosen:updated");

Thus remaining:

function carregarComboSetor( idSetor ){
    /* resto do codigo **/
          success : function (data) {

               /* resto do codigo **/

              $.each( data.setor, function (key, value) {
                 /* resto do codigo **/

              } );
             /* resto do codigo **/
              $('#setor').trigger("chosen:updated");
          }

      });

}

0

I think you should do the parse of json received by the function success:

function carregarComboSetor( idSetor ){
      $.ajax({
          url      : 'funcao/setor.php',
          type     : 'post',
          dataType : 'json',
          data : {
            acao : 'S',
            cdsetor : idSetor
          },
          success : function (data) {

              var data = JSON.parse(data); // esta linha

              var op = "<option value='0'>Selecione um setor</option>";
              $('#setor').append(op);
              $.each( data.setor, function (key, value) {
                  var option = "";
                  if( idSetor === value.codsetor ){
                      option = "<option value='"+ value.codsetor +"' selected>"
                                 + value.nmsetor
                               +"</option>";

                  }else{
                      option = "<option value='"+ value.codsetor +"'>"
                               + value.nmsetor
                               +"</option> ";
                  }
                 $('#setor').append(option);
              } );
              $('#setor').val( idSetor );
          }

      });

}
  • But the combobox only works if I put at the bottom of the page $('#setor').chosen();. Other than that my function works normally.

Browser other questions tagged

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