Problem to get the text attribute of a Select2

Asked

Viewed 636 times

-1

I’m not getting the text attribute selected in select2, I can only get the id.

<div class="col-sm-3">
   <input type="text" ui-select2="comboPasta()" ng-model="item.nomePasta" placeholder="Selecione">
</div>

This is my Javascript:

$scope.comboPasta = function () {

          var c = "rest/pastas";
          return c,{
              minimumInputLength: 0,
              ajax: {
                  url: "rest/pastas",
                  data: function (a, b) {
                      return {
                          nomePasta: a,
                          size: 10,
                          page: b - 1
                      }
                  },
                  results: function (a) {
                      for (var b = 0; b < a.length; b++) a[b].text = a[b].nomePasta;
                      return {
                          results: a,
                          more: 10 === a.length
                      }
                  }
              },
              initSelection: function (a, b) {
                  $.ajax("rest/pastas/" + a.val()).done(function (a) {
                      b({
                          id: a.id,
                          text: a.nomePasta
                      })
                  })
              }
          }
      };

And that’s the Function I’m trying to use to capture the field text.

$scope.addPermissoes = function addPermissoes() {
      var mensagem = "";
      if($scope.item.nomePasta == null){
        mensagem += "Informe o nome da pasta.\n";
      }
      if($scope.item.nomeSubPasta == null){
          mensagem += " Informe o nome da sub pasta.\n";
      }

      if(!$scope.item.leitura && !$scope.item.gravacao &&
          !$scope.item.exclusao && !$scope.item.compartilhar){
          mensagem += " Informar pelo menos uma permissão para as pastas.\n";
      }

      if(mensagem.length > 0){
          $notify.warning(mensagem);
      }else{
          $scope.perfil.perfilPastaDTOs || ($scope.perfil.perfilPastaDTOs = []);
          $scope.perfil.perfilPastaDTOs.push({
              nomePasta: $scope.perfil.perfilPastaDTOs.nomePasta,
              nomeSubPasta: $scope.item.nomeSubPasta,
              leitura: $scope.item.leitura,
              gravacao: $scope.item.gravacao,
              exclusao: $scope.item.exclusao,
              compartilhar: $scope.item.compartilhar
          })
      }
  }
  • Please be more specific, what code you are using to receive the text attribute?

  • 1

    it’s nice to share what you’ve done so far, not just play the code =(

1 answer

1

Based on this reply

you can pick up any property of the item using

var data = $('your-original-element').select2('data');   
if(data) {
  console.log(data.text);
}

only one detail regarding the version of Select2, if it is 4, then the code will be

var data = $('your-original-element').select2('data')
console.log(data[0].text);
console.log(data[0].id);

Browser other questions tagged

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