如何访问网格的过滤大小?

时间:2015-06-30 18:33:32

标签: javascript angularjs ng-grid

我正在使用ng网格。我正在使用过滤器。我的功能正常。现在我想(以编程方式)了解网格中剩余的项目数。

我在github上挖掘了源代码,发现该值存储为:

grid.renderContainers.body.visibleRowCache.length;

但是我还没有能够弄清楚如何访问该变量,特别是来自网格本身之外的JS的grid对象。

其他SO答案包括这样的代码:

angular.element($("#gridDiv")).scope()

虽然该代码有效,但它访问范围(我已经在JS中访问过),而不是网格中使用的数据。有没有办法访问网格内的javascript对象?

Sample Plunker

1 个答案:

答案 0 :(得分:1)

环顾gridOptions我发现了一个名为filteredRows的对象。 所以你可以得到你想要的长度:

$scope.gridOptions.ngGrid.filteredRows.length

请查看下面的演示或更新后的plunkr

onRegisterApi只是一个测试而且不需要。)



// main.js
var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
    $scope.myData = [{name: "Moroni", age: 50},
                     {name: "Tiancum", age: 43},
                     {name: "Jacob", age: 27},
                     {name: "Nephi", age: 29},
                     {name: "Enos", age: 34}];
                     
    $scope.filter = {filterText:''};
    $scope.gridOptions = {
      data: 'myData',
      enableFiltering: true,
    filterOptions: $scope.filter,
    showFilter: true,
    onRegisterApi: function( gridApi ) {
          $scope.gridApi = gridApi;
          $scope.gridApi.core.on.filterChanged( $scope, function() {
            console.log($scope.gridApi);
          });
    }
  };
  
  $scope.showRowCount = function() {
    console.log($scope.gridOptions.ngGrid.filteredRows.length);
  }
});

.gridStyle {
    border: 1px solid rgb(212,212,212);
    width: 400px; 
    height: 300px
}

<link href="http://angular-ui.github.com/ng-grid/css/ng-grid.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="http://angular-ui.github.com/ng-grid/lib/ng-grid.debug.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
      <input ng-model="filter.filterText"/>
      <button ng-click="showRowCount()">How many rows have been filtered</button>
        <div class="gridStyle" ng-grid="gridOptions"></div>
        currently visible items: {{gridOptions.ngGrid.filteredRows.length}}
    </div>
&#13;
&#13;
&#13;