Angular ui-grid双击行以打开弹出窗口以编辑行

时间:2015-01-28 20:24:52

标签: angularjs

版本:

我正在使用Angular ui-grid版本3.0.0-RC.18(http://ui-grid.info/)。

问题

我想在ui-grid表中实现双击事件。特别是,我想在双击一行时打开一个模态弹出窗口。

我尝试在https://github.com/angular-ui/ng-grid/issues/2228建议的rowTemplate定义中使用ng-dblclick指令,但是' dblclick'事件永远不会被解雇。

尽管如此,我找到了一个解决方案,但使用了我自己创建的指令。如果不创建指令,我可以做得更好吗?

任何评论都将不胜感激。

代码:

我在Controller的代码如下:

$scope.onDblClick = function(row) {
    var url = '//google.com';
    $window.open(url, "_blank", "height=600,width=800,toolbar=no,location=no,menubar=no,titlebar=no");
}

// Define the New Conflicts Simulation GRID behavior
$scope.myGridOptions = {
    showFooter: false,
    enableSorting: true,
    multiSelect: false,
    enableFiltering: true,     
    enableRowSelection: true, 
    enableSelectAll: false,
    enableRowHeaderSelection: false,  
    enableGridMenu: true,
    noUnselect: true,
    onRegisterApi: function (gridApi){
        $scope.gridApi = gridApi;
    },
    rowTemplate: "<div ng-dblclick=\"onDblClick(row)\" ng-repeat=\"(colRenderIndex, col) in colContainer.renderedColumns track by col.colDef.name\" class=\"ui-grid-cell\" ng-class=\"{ 'ui-grid-row-header-cell': col.isRowHeader }\" ui-grid-cell dbl-click-row></div>"            

}

(其中dbl-click-row表示我正在使用dblClickRow指令)

View中的代码如下:

<div id="myGrid" ui-grid="myGridOptions" ui-grid-selection ui-grid-resize-columns class="gridTable" ></div>

我在指令中的代码如下:

var angularStartDirectives = angular.module('angularStart.directives', []);     

angularStartDirectives.directive('dblClickRow', ['$compile', '$parse',  function($compile, $parse) {
  return {
    priority : -190, // run after default uiGridCell directive
    restrict : 'A',
    scope : false,

    compile: function($element, attr) {

        // Get the function at ng-dblclick for ui-grid
        var fn = $parse(attr['ngDblclick'], /* interceptorFn */ null, /* expensiveChecks */ true);

        return function ngEventHandler(scope, element) {

            element.on('dblclick', function(event) {

              var callback = function() {

                if ($scope.gridApi.grid.selection.lastSelectedRow)
                {
                    fn($scope, {$event:event, row: $scope.gridApi.grid.selection.lastSelectedRow.entity });
                }
              };

              $scope.$apply(callback);

            }
        )};

    }

    };
} ]);

3 个答案:

答案 0 :(得分:15)

好吧,我的问题在Github回答:

https://github.com/angular-ui/ng-grid/issues/2228#issuecomment-71912850

我的错误不是使用外部范围,而是尝试仅使用ng-dblclick来解决问题。

代码应该是这样的:

在控制器:

$scope.gridHandlers = { 

    onDblClick : function(row) {
        var url = '//google.com';
        $window.open(url, "_blank", "height=600,width=800,toolbar=no,location=no,menubar=no,titlebar=no");
   }
}

$scope.myGridOptions = {
    showFooter: false,
    enableSorting: true,
    multiSelect: false,
    enableFiltering: true,     
    enableRowSelection: true, 
    enableSelectAll: false,
    enableRowHeaderSelection: false,  
    enableGridMenu: true,
    noUnselect: true,
    onRegisterApi: function (gridApi){
        $scope.gridApi = gridApi;
    },
    rowTemplate: "<div ng-dblclick=\"getExternalScopes().onDblClick(row)\" ng-repeat=\"(colRenderIndex, col) in colContainer.renderedColumns track by col.colDef.name\" class=\"ui-grid-cell\" ng-class=\"{ 'ui-grid-row-header-cell': col.isRowHeader }\" ui-grid-cell ></div>"            

}

在视图中:

<div id="myGrid" ui-grid="myGridOptions" ui-grid-selection ui-grid-resize-columns class="gridTable" external-scopes="gridHandlers"></div>

ui-grid v3.0.0-rc.21更新:

考虑到不再支持externalScopes,现在appScopeProvider规则。

在视图中:

<div id="myGrid" ui-grid="myGridOptions" ui-grid-selection ui-grid-resize-columns class="gridTable" ></div>

在控制器中:

$scope.myGridOptions = {
    showFooter: false,
    enableSorting: true,
    multiSelect: false,
    enableFiltering: true,     
    enableRowSelection: true, 
    enableSelectAll: false,
    enableRowHeaderSelection: false,  
    enableGridMenu: true,
    noUnselect: true,
    onRegisterApi: function (gridApi){
       $scope.gridApi = gridApi;
    },
    appScopeProvider: { 
        onDblClick : function(row) {
           var url = '//google.com';
           $window.open(url, "_blank", "height=600,width=800,toolbar=no,location=no,menubar=no,titlebar=no");
         }
    },
    rowTemplate: "<div ng-dblclick=\"grid.appScope.onDblClick(row)\" ng-repeat=\"(colRenderIndex, col) in colContainer.renderedColumns track by col.colDef.name\" class=\"ui-grid-cell\" ng-class=\"{ 'ui-grid-row-header-cell': col.isRowHeader }\" ui-grid-cell ></div>"
}

以下是我在Plnkr中使用模态弹出窗口的示例(使用angular-ui-bootstrap完成):

http://plnkr.co/edit/cq7s9lKn90xTVgNxIC6b?p=preview

请注意,如果您使用较新版本的ui-bootstrap,则需要将上述plunkr中的$ modal重命名为$ uibModal。

答案 1 :(得分:2)

我已经使用了Aquiles解决方案并将范围缩小到appSCope,请参阅here

我重写了showInfo的代码变为$ scope:

$scope.showInfo = function(row) {
    var modalInstance = $modal.open({
      controller: 'InfoController',
      templateUrl: 'ngTemplate/infoPopup.html',
      resolve: {
        selectedRow: function () {                    
            return row.entity;
        }
      }
   });

   modalInstance.result.then(function (selectedItem) {
     $log.log('modal selected Row: ' + selectedItem);
   }, function () {
     $log.info('Modal dismissed at: ' + new Date());
  });
}

在gridOptions上刚刚使用了appScope:

$scope.gridOptions = {

   showFooter: true,
   enableSorting: true,
   multiSelect: false,
   enableFiltering: true,     
   enableRowSelection: true, 
   enableSelectAll: false,
   enableRowHeaderSelection: false,
   selectionRowHeaderWidth: 35,  
   noUnselect: true,
   enableGridMenu: true,
   columnDefs: [{displayName:'Name',field:'name'},{displayName:'Gender',field:'gender'},{displayName:'Company',field:'company'}],
   rowTemplate: "<div ng-dblclick=\"grid.appScope.showInfo(row)\" ng-repeat=\"(colRenderIndex, col) in colContainer.renderedColumns track by col.colDef.name\" class=\"ui-grid-cell\" ng-class=\"{ 'ui-grid-row-header-cell': col.isRowHeader }\" ui-grid-cell></div>"
};

我也添加了columnDef来表明rowTemplate不会干扰网格渲染。

答案 2 :(得分:0)

使用自定义rowTemplate的解决方案效果很好。我找到了一种更优雅的解决方案,可以将该事件添加到“ ui-grid / ui-grid-row”模板中:

rowTemplate : $templateCache.get('ui-grid/ui-grid-row').replace("<div ", "<div ng-dblclick=\"grid.appScope.onDblClick(row, $event)\" "),

双击事件处理程序将如下所示:

$scope.onRowDblClick = function (row) {
    if (row && row.entity) {
        //insert your custom code
    }
};

您必须将依赖项添加到控制器中的$ templateCache。