I am writing a simple datatable using react. After clicking a table cell, i need function to know exact row index and column index of it. In my case, i have a 3x3 table, with indexing from 0 - 2.
function getTable(){
let rowIndex = 0;
this.tableData.forEach(row => {
const rowValues = Object.values(row);
let colIndex = 0;
let rowData = [];
rowValues.forEach(colVal => {
rowData.push(
<td key={colIndex} onClick={() => this.enableEdit(rowIndex, colIndex)}>
{colVal}
</td>
);
console.log("row: " + rowIndex + ", column: " + colIndex);
colIndex++;
});
bodyRows.push(<tr key={rowIndex}>{rowData}</tr>);
rowIndex++;
});
return (
<table className="table table-dark">
<thead>
<tr>{headColumns}</tr>
</thead>
<tbody>{bodyRows}</tbody>
</table>
);
}
enableEdit(row, col){
console.log(row, col);
}
Console log, does everything right. My results of it go like:
row: 0, column: 0
row: 0, column: 1
row: 0, column: 2
row: 1, column: 0,
row: 1, column: 1,
row: 1, column: 2
...
Keys work well too, they are all unique as intended:
But, enableEdit()
function returns 3, 3
after clicking any cell (I guess its 3 because, that's the same value I get if i console.log after loops) , and that's my problem.
So what am I doing wrong? Why I don't get my expected results?