Array filter inside Angular object

Asked

Viewed 2,869 times

1

1 I have an array of travel objects 2 each trip has an array of users (travelers) I need to create a filter to list travelers I created a filter for the destinations, but for the users it’s not right. someone has some hint?

<script>
  var app = angular.module("viagem", []);

  app.controller("viagemController", function ($scope, $http) {

  $scope.viagens = [  
                     {  
                        "id":1,
                        "nomeTipoViagem":"Manutenção",
                        "destino":"Maranhâo",
                        "data":"2015-07-02",
                        "clientes":[  
                           {  
                              "nickname":"Cereais Comebem"
                           }
                        ],
                        "viajantes":[  
                           {  
                              "id_usuario":1,
                              "nome":"José Amaral"
                           },
                           {  
                              "id_usuario":2,
                              "nome":"Fernando Oliveira"
                           }
                        ],
                        "observacoes":null
                     }
                  ];
  });

</script>
  <body ng-app="viagem">
    {{viagens}}
    <table  ng-controller="viagemController" class="table table-bordered table-striped">
      <tbody>
        <tr>
          <th>Destino</th>
          <th>Viajantes</th>
        </tr>
        <tr>
          <td>
            <input type="text" class="form-control" ng-model="filtroDestino" ng-init="filtroDestino=''" />
          </td>

          <td>
            <input type="text" class="form-control" ng-model="filtroViajante" ng-init="filtroViajante=''" />
          </td>
        </tr>
        <tr ng-repeat="viagem in viagens  | filter:{destino:filtroDestino} | filter:{tipo:filtroTipo}">
          <td>{{viagem.destino}}</td>
          <td>
            <span ng-repeat="usuario in viagem.viajantes">{{usuario.nome}}                                                         <br />
            </span>
          </td>
        </tr>
      </tbody>
    </table>
  </body>

Plunker - Code

1 answer

0


You only need to make a simple change to your code:

<td>
  <span ng-repeat="usuario in viagem.viajantes | filter:{nome:filtroViajante}">
      {{usuario.nome}} <br />
  </span>
</td>

Plunker - Code

Browser other questions tagged

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