1
votes

I am having trouble getting the index of the selected cell in my table. I have attached a click function to each cell that is supposed to alert the index when pressed but I am unable to get the index properly. The index of the row is correct but the index of the column is always incorrect.

function doSomething(i) {
    setTimeout(function() {
        for (var j = 0; j < columnNum; j++) {
            oTable.getItems()[i].getCells()[j].$().parent().click(function() {
            alert(i+", "+j);
            });
        }
    }, i);
}

for (var i = 0; i < rowNum; i++) {
    doSomething(i);
}

Here is the full fiddle: https://jsbin.com/hecuhevawe/1/edit?html,css,js,output.

1

1 Answers

2
votes

Try to change the last loop, so you iterate through all cells and pass the row and column index to the doSomething function.

function doSomething(i,j) {
    setTimeout(function() {
            oTable.getItems()[i].getCells()[j].$().parent().click(function() {
               alert(i+", "+j);
            });
    }, i);
}

for (var i = 0; i < rowNum; i++) {
    for (var j = 0; j < columnNum; j++) {
      doSomething(i,j);
    }
}