0

So I'm working with the Handsontable jQuery plugin for a project at the moment, and I've written some custom functions to work with it.

The function I'm currently having trouble with is one I've written to return the currently selected cell(when the user has only selected one, not multiple, and yes that is checked for).

Here is my code:

function getCurrentCell(){
    var selection = $('div.current');
    var left = selection.offset().left;
    var right = left + selection.width();
    var top = selection.offset().top;
    var bottom = top + selection.height();
    $('div.active').find('table').find('td').each(function(){
        if($(this).offset().left >= left && $(this).offset().left <= right && $(this).offset().top >= top && $(this).offset().top <= bottom){
            return this;
        }
    });
    return false;
}

However, whenever I call the function such as:

var cell = getCurrentCell();

And then attempt to alert(cell) or console.log(cell), I get a false return value.

My initial thought would be that somehow the coordinates would be off, and therefore no element would be found matching the criteria, so I attempted to check by adding...

$(this).css('background-color', 'black');

...right before the return this line. That way, if the right table cell is found, it will show up on screen before actually returning in code. Funny thing is, the correct cell always has its background color changed properly. So, this function is finding the correct cell, and it is executing the code within the if loop, but when I try and capture the return value into a variable, that variable is always false.

Any help would be great! Thanks SO!

warpech
  • 6,293
  • 4
  • 35
  • 36
Jordan Foreman
  • 3,848
  • 7
  • 40
  • 65
  • I understand that your problem was with this function and that you found the solution to why your function wasn't working, but I highly recommend you use the solution posted by @warpech. Not only because Handsontable tries to keep logic and the DOM separate, but also because this will break if you have more than a few dozen rows on the screen. Virtual rendering will cause your selected cell to not be rendered, meaning that even if you have a cell selected, you won't be able to use the DOM to find it. Instead use `hotInstance.getSelected()`. – ZekeDroid Sep 19 '15 at 21:15

2 Answers2

3

You are using .each() with a function

.each(function(){
    ...
    return this;
});
return false;

This will return from the callback (and maybe stop the each-loop if this was false), but never break out and return from the outer getCurrentCell function! So, that one will always return false.

Quick fix:

var result = false;
<...>.each(function(){
    if (<condition>) {
        result = <found element>;
        return false; // break each-loop
    }
});
return result; // still false if nothing found
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2

Currently there is a better way to get currently selected in Handsontable. Just use the following methods from Handsontable:

handsontable('getSelected') - Returns index of the currently selected cells as an array [topLeftRow, topLeftCol, bottomRightRow, bottomRightCol]

handsontable('getCell', row, col) - Return element for given row,col

All methods are described here: https://github.com/warpech/jquery-handsontable

warpech
  • 6,293
  • 4
  • 35
  • 36
  • This is definitely the way to go. Trying to guess at the selected cell given the state of the DOM is not good practice so this solution should be used when trying to find the selected cell. – ZekeDroid Sep 19 '15 at 21:13