如何在页面加载中选择UIGrid中的行?

时间:2018-03-30 10:43:56

标签: angular-ui-grid ui-grid

我想在页面加载时在UI网格中选择一行。我试过这段代码,但是我遇到了错误;

TypeError:newRawData.forEach不是函数

$scope.deviceGrid = { 
data:'deviceDetails',
enableRowSelection: true, 
enableRowHeaderSelection: false, 
enableSelectAll: false,
multiSelect: false,
noUnselect: true,
minRowsToShow :10,


onRegisterApi: function (gridApi) { 
    $scope.gridApi = gridApi;   
    $scope.gridApi.grid.modifyRows($scope.deviceGrid.data); 
    $scope.gridApi.selection.selectRow($scope.deviceGrid.data[0]);
 gridApi.selection.on.rowSelectionChanged($scope, function (row) {
    var msg = 'row selected ' + row.isSelected;
   console.log(row.entity.devHardwareId);
   $scope.devHardwareId= row.entity.devHardwareId;
    });
}};

他们是另一种方法吗?

1 个答案:

答案 0 :(得分:1)

您有两种选择:

1)将数据设置为网格后使用$ timeout

$timeout(function() {
        if($scope.gridApi.selection.selectRow){
          $scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);
        }
      });

2)用于从gridApi

渲染的行
onRegisterApi: function (gridApi) {
                $scope.gridApi = gridApi;

                gridApi.core.on.rowsRendered($scope, function () {
                     gridApi.selection.selectRow($scope.gridOptions.data[0]);                           
                });