【问题标题】:Angular UI Grid - Click event on selected rowAngular UI Grid - 选定行上的单击事件
【发布时间】:2017-10-26 06:57:12
【问题描述】:

目标

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

当前方法

我使用以下配置代码来生成网格:

$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),即使已经选择了行。

【问题讨论】:

    标签: javascript angularjs angular-ui-grid


    【解决方案1】:

    对于要选择的行,当它被点击时,我使用以下:

    对所有列使用 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]);
    };
    

    我还定义了多选为真

    $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()
    

    【讨论】:

      【解决方案2】:

      将对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

      【讨论】:

        【解决方案3】:

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

        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 事件。

        【讨论】:

          猜你喜欢
          • 2019-10-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-15
          • 2017-08-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多