Filter for a multidimensional matrix

Asked

Viewed 57 times

1

How could I do to bring everything in the view and filter in 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 JSFIDDLE

1 answer

1


One option is to create a function to take care of that filter. I updated your Jsfiddle with a possible solution. In short, I adjusted its function filterTurma() thus:

$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 yet an option "All" with the value '!!'. In the filter, we only check whether the current value of the item selected in the 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 we do not use parenthesis in the name of the method (the Angularjs will automatically pass the current user in the iteration to the method), plus I kept the original filter by name, ie both filters complement each other (and you can match as many filters as you want).


In its original Jsfiddle, the object dadosUserAndTurma had a user Priscilla containing class 2 with the value 1. In my updated fiddle I changed this value to 2, because it seemed to be correct, maybe you need to adjust this also in your original code.

Browser other questions tagged

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