如何在select中设置Array

时间:2015-07-22 12:47:53

标签: javascript arrays

我使用Selectize.js,我在一种搜索中有一个字段,其中的值是根据数组设置的。我得到了一个请求的数组,并希望在我的选择选项中设置他。我怎么能这样做?

HTML

<div id="wrapper">
            <h1>Selectize.js</h1>
            <div class="demo">
                <div class="control-group">
                    <label for="select-tools">Tools:</label>
                    <select id="select-tools" placeholder="Buscar ag&ecirc;ncia..."></select>
                </div>
            </div>
        </div>

这有效

agenciasAPI.getAgencias().success(function (data) {
    var embedded = data._embedded;
    $scope.listaAgencias = embedded.agencias;
    alert(JSON.stringify($scope.listaAgencias));
  }).catch(function (error) {
    alert("Erro ao obter listagem de agencias");
    console.log(JSON.stringify(error));
  });


  $('#select-tools').selectize({
    maxItems: null,
    valueField: 'nome',
    labelField: 'nome',
    searchField: 'nome',
    options: [
      {nome: 'Spectrometer'},
      {nome: 'Star Chart'},
      {nome: 'Electrical Tape'}
    ],
    create: false
  });

enter image description here

但我想要这样的东西......

  agenciasAPI.getAgencias().success(function (data) {
    var embedded = data._embedded;
    $scope.listaAgencias = embedded.agencias;
    alert(JSON.stringify($scope.listaAgencias));
  }).catch(function (error) {
    alert("Erro ao obter listagem de agencias");
    console.log(JSON.stringify(error));
  });


  $('#select-tools').selectize({
    maxItems: null,
    valueField: 'nome',
    labelField: 'nome',
    searchField: 'nome',
    options: $scope.listaAgencias,
    create: false
  });

但不起作用 enter image description here

我的$ scope.listaAgencias是

[
   {
      "nome":"Agencia um",
      "createdBy":"anonymousUser",
      "lastModifiedBy":"anonymousUser",
      "createdAt":"2015-07-21T12:15:36.369+0000",
      "lastModified":"2015-07-21T12:15:36.369+0000",
      "_links":{
         "self":{
            "href":"http://localhost:8181/api/agencias/55ae37e8ccf2070af2e5ab1c"
         }
      }
   },
   {
      "nome":"Agencia dois",
      "createdBy":"anonymousUser",
      "lastModifiedBy":"anonymousUser",
      "createdAt":"2015-07-21T12:15:41.286+0000",
      "lastModified":"2015-07-21T12:15:41.286+0000",
      "_links":{
         "self":{
            "href":"http://localhost:8181/api/agencias/55ae37edccf2070af2e5ab1d"
         }
      }
   }]

1 个答案:

答案 0 :(得分:0)

我刚刚进入我的GET请求并且工作

  agenciasAPI.getAgencias().success(function (data) {
    var embedded = data._embedded;
    $scope.listaAgencias = embedded.agencias;


      $('#select-tools').selectize({
        maxItems: null,
        valueField: 'nome',
        labelField: 'nome',
        searchField: 'nome',
        options: embedded.agencias,
        create: false
      });

    alert(JSON.stringify($scope.listaAgencias));
  }).catch(function (error) {
    alert("Erro ao obter listagem de agencias");
    console.log(JSON.stringify(error));
  });

enter image description here