Skip to content

Loading…

Questions: How get select rows or columns index? #44

Closed
kztma opened this Issue · 12 comments

6 participants

@kztma

I would like to ask you the following questions;

How get select rows or columns index?
Do you have callback method return select rows or columns array index?

@azizasm

To select rows or columns index use selectCell and specify the index. Example below to select the row/col 0,0

container.handsontable("selectCell", 0, 0);

The callback method described in Demo Page- Save

@kztma

Thank you for @azizasm !

I understand you are saying.
But I would like to know column and row index selecting now.

@bruceh48

This is how I've done it:

var selection = $("#exampleGrid").handsontable('getSelection');

selection is from col1,row1 to col2, row2

where

col1 = selection.start().col;
row1 = selection.start().row;

col2 = selection.end().col;
row2 = selection.end().row;

Hope that helps

@kztma

thanks @bruceh48 !

I tryed it but it occurred javaScript Error.
The details of Error: $(this).data("handsontable")[action] is undefined

Perhaps you could go into a little more detail.

@bruceh48

Oops my mistake..

I had forgotten that I had added an additional function to the handsontable.js file.

Its,
/**
* Returns the selection
*/
this.getSelection = function()
{
return selection;
};

I added it on line 2079, just after the function that starts with a line line that says :
this.selectCell = function

Without this, I could not "reliably" get the selection.

Its works well for me.

Hope that helps

@kztma

thanks @bruceh48 !

Mr. @warpech !
I'm glad you add above-mentioned @bruceh48 's code!
I beg your kindness.

@bgl3317

If you don't want to mess with the source code, all you really need is a little jQuery handler:

var rowIndex;
$('#yourTable tr').click(function () { rowIndex = $(this).index() });

You could modify this just a bit to capture a row select from one of the arrow keys as well.

@warpech

Hi guys, I have created a callback called onSelection. Use it as shown in http://warpech.github.com/jquery-handsontable/demo/conditional.html:

container.handsontable({
  ...

  onSelection: function(row, col, row2, col2) {
    var meta = container.handsontable('getCellMeta', row2, col2);
    if(meta.isWritable == false) {
      container.handsontable('updateSettings', {fillHandle: false});
    }
    else {
      container.handsontable('updateSettings', {fillHandle: true});
    }
  },

  ...
}

Do you think @bruceh48 method still would be handy? I am thinking about including it in the core

@kztma

thanks @warpech !
I've found what I want to do.

@kztma kztma closed this
@littley

@warpech

Yes, bruceh48's method would be very useful.

@littley

@warpech @bruceh48

The code added by @bruceh48 has unpredictable behaviour depending on which direction the user makes his selection. For example: if the user makes a selection from a,b to c,d, he gets the coordinates (a,b), (c,d). However, if the user selects from b,c to a,d (the exact same area), he gets different coordinates.

The following code fixes this error and is intended to replace the code given by @bruceh48

this.getSelected = function() { 
    var x1 = selection.start().row;
    var y1 = selection.start().col;
    var x2 = selection.end().row;
    var y2 = selection.end().col;

    if( (x1 <= x2 && y1 < y2) || (x1 < x2 && y1 <= y2) || (x1 == x2 && y1 == y2)) {
        return [x1,y1,x2,y2];
    }
    else if( (x1 >= x2 && y1 > y2) || (x1 > x2 && y1 >= y2)) {
        return [x2,y2,x1,y1];
    }
    else if(x1 < x2 && y1 > y2) {
        return [x1,y2,x2,y1];
    }
    else if(x1 > x2 && y1 < y2) {
        return [x2,y1,x1,y2];
    }
};

The function $("#exampleGrid").handsontable('getSelected') now returns an array containing the coordinates, regardless of the direction of selection. [row1,col1,row2,col2]

I have have created a fork with this code in the appropriate place. See https://github.com/littley/jquery-handsontable

@warpech

The method getSelected is now included in core. Thanks guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.