Search code examples
javascriptjqueryscrollhandsontable

Handsontable - Disable auto scroll up when pasting into a cell


When I paste into a cell which is nearing the top of the page in a handsontable table, the page automatically scrolls up to center the window on the cell.

I've tried to locate the code causing this in jquery.handsontable.full.js in the hopes of disabling it, but haven't been successful.

Some help would be greatly appreciated!


Solution

  • I've been struggling with the same problem for the past weeks until today, I managed to fix it without editing the library.

    Where I initialize the handsontable I added the following:

    $(document).ready(function () {
        var position_Y_before = null;
    
        $(".handsontable").handsontable({
            //some code here for initializing the table and its functions
    
            //Then I added this callback function
            beforeKeyDown: function(e) {
                position_Y_before = window.pageYOffset || 0;
            };
    
        });
    
        //Here we prevent from scrolling to top of page after pasting to handsontable with cmd+v or ctrl+v
        $(window).scroll(function(){
            if(position_Y_before != null){
                window.scrollTo(0, position_Y_before);
                position_Y_before = null;
            }
        });
    
    });
    

    This works for me at least, hope it helps you as well!