问题
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 )">
回答1:
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
.
回答2:
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
来源:https://stackoverflow.com/questions/34812402/get-the-length-of-list-shown-by-ng-show