Get the length of list shown by ng-Show

前端 未结 2 1783
渐次进展
渐次进展 2021-01-06 23:52

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.

E

相关标签:
2条回答
  • 2021-01-07 00:33

    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

    0 讨论(0)
  • 2021-01-07 00:35

    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.

    0 讨论(0)
提交回复
热议问题