Angular UI Grid - 所选行上的Click事件

时间:2016-11-25 09:48:37

标签: javascript angularjs angular-ui-grid

:定位

我有一个UI网格。当我点击一行时,它应该被选中,并且应该调用以行作为参数的函数。

当前方法

我使用以下配置代码生成Grid:

$scope.gridOptions = {
        enableFiltering: true,
        enableRowHeaderSelection: false,
        enableRowSelection: true,
        multiSelect: false,
        noUnselect: true,
        onRegisterApi: function (gridApi) {
            $scope.gridApi = gridApi;
            $scope.gridApi.selection.on.rowSelectionChanged($scope, function (row) {
                var name = row.entity.name;
                $scope.addToQueue(name);
            });
        }
    };

问题

当我实际更改选择时(作为函数的名称建议),上面的代码很有效。但应该可以多次向队列添加一行。因此,即使已经选择了行,我也想调用$scope.addToQueue(name)

3 个答案:

答案 0 :(得分:3)

对于要选择的行,单击它时,我使用以下内容:

对所有列使用selectionCellTemplate:

 var selectionCellTemplate = '<div class="ngCellText ui-grid-cell-contents">' +
               ' <div ng-click="grid.appScope.rowClick(row)">{{COL_FIELD}}</div>' +
               '</div>';

 $scope.gridOptions.columnDefs = [        
    { name: 'name', displayName: 'Name', width: '15%', cellTemplate: selectionCellTemplate },       
   ];

然后将rowClick()方法定义为:

 $scope.rowClick = function (row) {       
    var index = row.grid.renderContainers.body.visibleRowCache.indexOf(row);
    $scope.gridApi.selection.selectRow($scope.gridOptions.data[index]);
};

我还将multiselect定义为真实

$scope.gridOptions.multiSelect = true;

因此,行单击将选择该行并将其添加到所选行。您可以访问这些选定的行(它为每行选择/取消选择触发):

$scope.gridOptions.onRegisterApi = function (gridApi) {
    //set gridApi on scope
    $scope.gridApi = gridApi;        
    gridApi.selection.on.rowSelectionChanged($scope, doSelection);
};

function doSelection(row) {      
    _.each($scope.gridApi.selection.getSelectedRows(), function (row) {
        //Do something //It is triggered for each row select/unselect         
    });
}

或者可以随时访问所选行:

  $scope.gridApi.selection.getSelectedRows()

答案 1 :(得分:1)

将对 addToQueue 的调用移至gridApi.grid.element.on('click'...)函数,并将存储在gridApi.selection.on.rowSelectionChanged函数中:

$scope.gridOptions.onRegisterApi = function (gridApi) {
  //set gridApi on scope
  $scope.gridApi = gridApi;
  gridApi.selection.on.rowSelectionChanged($scope, function (row) {
    $scope.gridApi.grid.appScope.lastSelectedRow = row;
  });

  gridApi.grid.element.on('click', function (ev) {
    if ($scope.gridApi.grid.appScope.lastSelectedRow) {
      // affect only rows (not footer or header)
      if (ev.target.className.includes('ui-grid-cell-contents')) {
        var name = $scope.gridApi.grid.appScope.lastSelectedRow.entity.name;
        $scope.addToQueue(name);
      }
    }
  });
};

$scope.addToQueue = function addToQueue (name) {
  console.log('addToQueue fired, name = ' + name);
};

plunker:http://plnkr.co/edit/qtXJ7MUy35QRjYXkEXuG?p=preview

答案 2 :(得分:1)

批准的答案对我有用,但对于这里的一个小错误......

var selectionCellTemplate = '<div class="ngCellText ui-grid-cell-contents">' +
           ' <div ng-click="grid.appScope.rowClick(row)">{{COL_FIELD}}</div>' +
           '</div>';

我需要将其更改为......

var selectionCellTemplate = '<div class="ngCellText ui-grid-cell-contents"
           ng-click="grid.appScope.rowClick(row)">' +
           '<div>{{COL_FIELD}}</div>' +
           '</div>';

您会注意到我将ng-click移动到父div。 这是必需的,好像一个单元格是空的,ng-click事件不会触发。