ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

Intercepting a double-click event

Open mbcooper opened this issue 12 years ago • 22 comments

My client has a need to do some special processing when they double-click a row. It seems to be consumed somehow ... Is there a way i can register an external callback on an event like a double-click?

--mike

mbcooper avatar Nov 25 '14 16:11 mbcooper

Answered my own question. Like v2.0, you add a row-template with an ng-dblclick directive pointing to your function.

The new "secret" in v3.x is that you have to escape the isolated state of ui-grid (a good thing)! with

<div ng-dblclick="getExternalScopes().showMe()"  external-scopes="myViewModel">Click here</div>

The external-scopes methods (in the event fn and as an attribute) are neede to make it all connect.

mbcooper avatar Nov 25 '14 22:11 mbcooper

Hello @mbcooper,

I'm having the same need, where did you add this code? Could you please copy a part of your code showing how you are handling the double click?

Thanks!

bielmenezes avatar Nov 26 '14 19:11 bielmenezes

In the column definitions as a rowTemplate

mbcooper avatar Nov 26 '14 20:11 mbcooper

Ok. I got it.

In version 2.0 I used to use a plugin that handled the double click, but reading the docs of 3.0 seens that the old plugin architecture is not available anymore.

bielmenezes avatar Nov 26 '14 20:11 bielmenezes

I found 3.x to be cleaner in many ways. Isolated scope is good, and a lot more built-in. Not having to load jQuery is a big plus!

mbcooper avatar Nov 26 '14 20:11 mbcooper

I am having the same need also. But, I am using Angular ui-grid version 3.0.0-RC.18.

I want to implement a double-click event in an ui-grid table. In particular, I want to open a modal pop-up when double-clicking at a row.

I tried to use a ng-dblclick directive inside the rowTemplate definition as is suggested by mbcooper, but 'dblclick' event is never fired.

Nevertheless, I found a solution, but using a directive created by my own. Can I do it better, without creating a directive?

Any comment would be appreciated.

CODE:

My code at the Controller is as follows:

$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>"            

}

(Where dbl-click-row indicates I am using the dblClickRow directive)

My code at the View is as follows:

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

My code at the Directive is as follows:

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);

            }
        )};

    }

    };
} ]);

ComeOutToPlay avatar Jan 28 '15 20:01 ComeOutToPlay

Hello @ComeOutToPlay,

I'm resolving this need with the @mbcooper suggestion and it's working perfect for me.

Firstly, you have to informe the external-scope at the grid declaration, as follow:

<div ui-grid="gridOptions" ui-grid-paging  external-scopes="gridHandlers"/>

Secondly, you have to declare in your controller the 'external scope' (the gridHandlersin my case) attribute and its elements, as follow:

 $scope.gridHandlers = {
        onGridDoubleClick : function(rowItem) {
            //your function goes here
        }
    }

And the final step is to declare the row template with the ng-dblclick attribute in the gridOptions:

    $scope.gridOptions = {
           ...
        rowTemplate: '<div ng-dblclick="getExternalScopes().onGridDoubleClick(row)" ng-repeat="col in colContainer.renderedColumns track by col.colDef.name" class="ui-grid-cell" ui-grid-cell></div>',
}

I hope it can help you.

bielmenezes avatar Jan 28 '15 20:01 bielmenezes

Thank you!

My mistake was not to use external-scopes, and try to solve the problem with ng-dblclick only.

ng-dblclick is never fired if you do not use external-scopes.

Thank you again for your clear explanation!!

ComeOutToPlay avatar Jan 29 '15 13:01 ComeOutToPlay

FYI, in master, the external-scopes attribute and getExternalScopes() is no more.

Now, the same scope that you assign gridOptions is assigned to grid.appScope. If that doesn't work for your app, there is now gridOptions.appScopeProvider = whateverYouWant. appScopeProvider will then be assigned to grid.appScope.

This will be detailed in the release notes for the next RC

swalters avatar Jan 29 '15 16:01 swalters

Hello @swalters,

Thanks for let us know this change before our code breaks!!

bielmenezes avatar Jan 29 '15 17:01 bielmenezes

Considering the appScopeProvider. It is already released

In the view:

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

In the controller:


$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>"
}

ComeOutToPlay avatar Feb 10 '15 22:02 ComeOutToPlay

how to make a button click display a modal popup inside uigrid.

tasnimpulikkal avatar Apr 30 '15 15:04 tasnimpulikkal

@tasnimpulikkal http://brianhann.com/create-a-modal-row-editor-for-ui-grid-in-minutes/ this has instructions on opening a modal by clicking a button, specifically for a row editor but you could use the modal for whatever purpose you want.

c0bra avatar Apr 30 '15 16:04 c0bra

@c0bra Thanks a ton for this share. Quick question. How can i disable the forms in the modal popup and just display one single field for example outside my forms ie eg: message field.

tasnimpulikkal avatar May 01 '15 00:05 tasnimpulikkal

You can alter the modal template and put whatever you want in it. The row should be available in the modal controllers scope.

On Thu, Apr 30, 2015, 7:55 PM tasnimpulikkal [email protected] wrote:

@c0bra https://github.com/c0bra Thanks a ton for this share. Quick question. How can i disable the forms in the modal popup and just display one single field for example outside my forms ie eg: message field.

— Reply to this email directly or view it on GitHub https://github.com/angular-ui/ng-grid/issues/2228#issuecomment-98011395.

c0bra avatar May 01 '15 01:05 c0bra

@ ComeOutToPlay,

I used above code for popup window generation. when I double click on grid it's opening google window in new page. how can I open grid row details popup page. I am new to angularjs, can you send me a sample code, that will help me more. Thank you.

asiam9 avatar May 21 '15 06:05 asiam9

Hello @asiam9

When I show popup details, I use angular-ui-bootstrap. Please refer to this link:

https://angular-ui.github.io/bootstrap/

Here is an example:

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

I hope it works for you!

ComeOutToPlay avatar May 21 '15 14:05 ComeOutToPlay

Thanks a lot! AppScopeProvider seems more useful. @ComeOutToPlay

savokiss avatar Oct 14 '15 07:10 savokiss

I have this implementation for a modal popup on double click. The problem I'm having now is adding a bootstrap daterange picker. I don't want to use the native datepicker. The fact that I'n assigning the appScope a function in the gridOptions is not allowing me to have a second assignment for bootstrap datepicker. How can I work around this or is there a better way to implement both,

Here is my stackoverflow question with more details.

Here is an example plunker with the problem I'm having.

Plunker

euvin3582 avatar Mar 03 '16 15:03 euvin3582

Thanks a lot for your comment. @ComeOutToPlay . I use your writing. :) but I have some question. Why use appScopeProvider option? I search that option on docs (http://ui-grid.info/docs/#/api/ui.grid.class:GridOptions) and find that by default, the parent scope of the ui-grid element will be assigned. So in my case, I use just parent scope's function.

What is better benefit to use appScopeProvider option? Thanks again bro.

cookatrice avatar Nov 21 '17 07:11 cookatrice