Filter for a multidimensional array

1

How could I do to bring everything in the view and filter on a combobox by the class option?

$scope.dadosUserAndTurma = [
                  {
                    idUser:20,
                    nome: "Carlos",
                        turmas: [
                             {turma: "Turma 1", value: 1},
                                 {turma: "Turma 2", value: 2},
                                 {turma: "Turma 3", value: 3}
                               ]
                  },
                  {
                    idUser:21,
                    nome: "Luiz",
                        turmas: [
                             {turma: "Turma 1", value: 1},
                                 {turma: "Turma 3", value: 3}
                               ]
                  },
                  {
                    idUser:22,
                    nome: "Priscilla",
                        turmas: [
                             {turma: "Turma 2", value: 1}
                               ]
                  } ,
                 {
                    idUser:24,
                    nome: "Pedro",
                        turmas: []
                  }
              ];

}  

Example in the JSFIDDLE

    
asked by anonymous 19.05.2017 / 17:01

1 answer

1

One option is to create a function to take care of this filter. I updated your JSFiddle with a possible solution. In short, I've adjusted your filterTurma() function this way:

$scope.filterTurma = function(usuario) {

    // Caso o valor seja '!!' (valor que você está atribuindo para "Todos"),
    // apenas retornar 'true' para todos os casos continuarem aparecendo...
    if ($scope.search_turmas.turmas.value == '!!')
        return true;

    // Do contrário, iterar as turmas do usuário, e verificar se ao menos
    // uma delas corresponde com a turma selecionada no combobox...
    for (var i = 0; i < usuario.turmas.length; i++) {
        var turma = usuario.turmas[i];
        if (turma.value == $scope.search_turmas.turmas.value)
            return true;
    }

    // caso nenhuma tenha correspondido, retornar 'false' para ocultar esse usuário...
    return false;
};

Summarizing the code, in your combo you have the classes, and still an "Everyone" option with the value '!!' . In the filter, we only check if the current value of the selected item in combobox is this, otherwise we check if at least one class corresponds to the selected class. In your HTML, to apply this filter, just do so:

<tr ng-repeat="usuario in dadosUserAndTurma | filter:{nome:search_nome} | filter:filterTurma">

Note that I do not use parentheses in the method name ( angularjs will automatically pass the current user into the iteration for the method), in addition I kept the original filter by name, that is, both filters complement each other (and you can combine as many filters as you like).

  

In your original JSFiddle, the object dadosUserAndTurma had a user Priscila containing class 2 with value 1. In my updated fiddle I changed this value to 2, since it seemed to be the correct, you may need to adjust this also in your original code.

    
20.05.2017 / 02:31