【发布时间】:2019-01-21 22:29:48
【问题描述】:
版本:
我正在使用 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);
}
)};
}
};
} ]);
【问题讨论】:
标签: angularjs