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

Enter key press in ngGrid

Open sagarwasule opened this issue 13 years ago • 6 comments

I require a functionality due to which I will be able to tab out to adjacent cell on enter button pressed in editable cell in ngGrid

In short I want "enter" key press same as "tab" key press. Please help. Thank you

sagarwasule avatar Aug 29 '13 12:08 sagarwasule

Enter goes down to the cell in the next row. This was done to emulate Excel.

Enter - Down Shift-Enter - Up Tab - Right shift-Tab - left

You will need to do some customization of the key-handler to accomplish a different behavior

swalters avatar Aug 29 '13 13:08 swalters

thanks swalters for your response. Any pointers for the same ? I wrote a keyhandler to handle Enter keypress, but how to make it to tab to the next cell.

sagarwasule avatar Aug 29 '13 13:08 sagarwasule

It will be also good if I can close the cell from edit mode on enter button click. Can you please provide any pointer for this ? Thanks

sagarwasule avatar Aug 29 '13 13:08 sagarwasule

I would start with navigation.js in the src folder. I didn't actually code the key navigation so I don't know the specifics.

Maybe you could just override the key event in a custom edit directive? Turn Enter into a Tab? I have no idea :)

Just curious, but why do you need Enter to go to next col to right? Why isn't Tab ok for that?

swalters avatar Aug 29 '13 17:08 swalters

Just in case the user presses enter key, then he won't get shifted to the row below but should get tab to the next column. Any idea if I can call blur event on enter keypress.

sagarwasule avatar Aug 30 '13 14:08 sagarwasule

I did this using a custom directive and few changes in ui-grid-unstable.js

U need to call this directive in ur editable cell template:

angular.module('blablaApp')
.directive('handleEnter', function () {
    return {
        require: '?ngModel',
        link: function (scope, element, attrs, ngModelCtrl) {


              $(element).on('keypress', function (e) {

                var code = e.keyCode || e.which;
                if (code == 13) { // ENTER pressed
                    e.preventDefault();
                    document.activeElement.blur();
                }
                else {
                    //any custom code. I am using regex to avoid alphabets
                    if (String.fromCharCode(e.keyCode).match(/[^0-9\.]/g)) return false;
                }
            });
        }
    };
});

And in ui-gird-unstable.js file modify the following code to make the behavior of enter as tab while traversing through row:

 getDirection: function (evt) {
          if (evt.keyCode === uiGridConstants.keymap.LEFT ||
            (evt.keyCode === uiGridConstants.keymap.TAB && evt.shiftKey) ||
            (evt.keyCode === uiGridConstants.keymap.ENTER && evt.shiftKey)) {
            return uiGridCellNavConstants.direction.LEFT;
          }
          if (evt.keyCode === uiGridConstants.keymap.RIGHT ||
            evt.keyCode === uiGridConstants.keymap.TAB || evt.keyCode === uiGridConstants.keymap.ENTER) {
            return uiGridCellNavConstants.direction.RIGHT;
          }
  if (evt.keyCode === uiGridConstants.keymap.UP) {
            return uiGridCellNavConstants.direction.UP;
          }

          if (evt.keyCode === uiGridConstants.keymap.PG_UP){
            return uiGridCellNavConstants.direction.PG_UP;
          }

          if (evt.keyCode === uiGridConstants.keymap.DOWN) {
            return uiGridCellNavConstants.direction.DOWN;
          }

          if (evt.keyCode === uiGridConstants.keymap.PG_DOWN){
            return uiGridCellNavConstants.direction.PG_DOWN;
          }

          return null;
        },

That' it. Hope this helps. Happy codding :)

mehakjain avatar Jul 29 '15 08:07 mehakjain