select 过滤后第一个可见行 ui-grid

select first visible row after filter ui-grid

我无法在应用过滤器后强制 select 第一行。因此,当我将页面加载到 select 第一行时,我使用:

      gridApi.selection.selectRow($scope.gridOptions.data[0]);

这来自 API 文档,很清楚。 现在,我正在尝试 select 过滤器后的第一行。 我有来自官方文档的 singleFilter 函数

 $scope.singleFilter = function( renderableRows ){
           var matcher = new RegExp($scope.filterValue);
           renderableRows.forEach( function( row ) {
               var match = false;
               [
              'name', 'company', 'email'
               ].forEach(function( field ){
                   if (field.indexOf('.') !== '-1' ) {
                       field = field.split('.');
                   }
                   if ( row.entity.hasOwnProperty(field) && row.entity[field].match(matcher) || field.length === 2 && row.entity[field[0]][field[1]].match(matcher)){
                       match = true;
                   }

               });
               if ( !match ){
                   row.visible = false;
               }
           });
           var rows = $scope.gridApi.core.getVisibleRows();

           var first =  function(array, n) {  
                  if (array == null){   
                  return void 0;
                  }
                  if (n == null) {  
                  return array[0]; 
                  }
                  if (n < 0) {
                  return [];
                  }
                  return array.slice(0, n); 
           }; 

           console.log(first(rows))
           $scope.gridApi.selection.selectRow(first(rows));

           return renderableRows;
       };

我在哪里获得可见行的长度

           var rows = $scope.gridApi.core.getVisibleRows();

通过简单的脚本我得到了第一行

       var first =  function(array, n) {  
              if (array == null){   
              return void 0;
              }
              if (n == null) {  
              return array[0]; 
              }
              if (n < 0) {
              return [];
              }
              return array.slice(0, n); 
       }; 
       console.log(first(rows))

然后我尝试应用 selection

 $scope.gridApi.selection.selectRow(first(rows));

可惜没有成功。我的错误在哪里?我感谢任何帮助。 My plunker

我在下面创建了一个工作插件。

这不起作用的原因是因为您获得的可见行是所有行,而不仅仅是过滤后的行。所有行的原因是因为您在返回过滤器之前调用它们。我已经使用我们目前所了解的知识创建了逻辑,这就是函数完成后返回的内容。

http://plnkr.co/edit/LIcpOs7dXda5Qa6DTxFU

 var filtered = [];
 for (var i = 0; i < renderableRows.length; i++) {
   if (renderableRows[i].visible) {
     filtered.push(renderableRows[i].entity)
   }
 }

 if (filtered.length) {
   $scope.gridApi.selection.selectRow(filtered[0]);
 }