2

I'm using Vraptor + Angular, and the question is simple. I hide some rows in my table with ng-show, but I want to take the count/length of the actual lines shown.

Example: I have 10 records on $data. But when I use ng-show, it just shows 5 records. When I use $data.length, it returns 10, but I want it to return 5.

Table:

<table data-ng-table="$tableUsuario.params">
    <tbody >
        <tr data-ng-repeat="usuario in $data | filter:search:strict" 
         data-ng-show="usuario.ativo == filtroAtivo && (filtroPerfil == null || usuario.perfil == filtroPerfil )">
JSON C11
  • 11,272
  • 7
  • 78
  • 65
  • 1
    it's not exactly clear what you are trying to accomplish here. Using a `filter` and `ng-show` together in the same `ng-repeat` is a bit redundant. If you *only* use a filter, you can alias the filter results, but if you want to reduce the count further by whatever `ng-show` is filtering out as well, you'll have to write your own algorithm to figure out the count. – Claies Jan 15 '16 at 13:55

2 Answers2

0

I'm a little confused on the question, but could you use:

$data.length to get the total count of your data.

The $index of the ng-repeat to get the current row/length.

You could then use both values in an ng-show.

Chewpers
  • 2,430
  • 5
  • 23
  • 30
  • Exemple: I have 10 records on $data. But when i use ng-show, just show 5 records. When i use $data.length, return 10. But i wanna that return 5. Get it? – Samuel Oliveira Jan 15 '16 at 13:57
  • Ah, OK. Then like @vittore mentioned, you might need your own custom filter or to create a new scope variable. – Chewpers Jan 15 '16 at 14:01
0

Here is the thing, on one hand you can check DOM and see how many visible rows you have, that will answer your question, but that is really bad solution, that will make performance of your angular app worse and not really angular-ish way to do things.

Another option you have is to create your own filter and use that, in this case you will be able to tell how many items you have within the filter.

Third option which is the best in my opinion is to create another scope variable and fill it in only with filtered items, of course you will need to maintain it in sync with your main list and you can do it using $watch and $watchCollection methods of your scope

.... 
$scope.$data = [ ...]

function ativoOrPerfil(item) {
  return item.ativo == filtroAtivo && (filtroPerfil == null || item.perfil == filtroPerfil )

}

function updateFiltered() {
   $scope.$dataFiltered = $scope.$data.filter(ativoOrPerfil)
}

$scope.$watch('filtroAtivo', updateFiltered)
$scope.$watch('filtroPerfil', updateFiltered)

Although keep in mind that since you are using strict filter and then your own filter, you will need to tweak updateFiltered to include all 3 conditions you have - filter, ativo and peril

vittore
  • 17,449
  • 6
  • 44
  • 82