0

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:

results

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?

8
  • When you are iterating using forEach you are increasing your col and row index for every iteration. So the value 3,3 is expected. You probably should move your bodyRows to local react state in which each cell will have its own row and col index. Currently your this.enableEdit(rowIndex, colIndex) sends overwritten values of indices. Commented Mar 23, 2019 at 14:06
  • @UtsavPatel Sorry, i don't seem to understand. Why are keys unique and console log right, but onClick's parameters are of post-iteration? stackoverflow.com/questions/40044861/… Judging by this link, they are doing something similar and successfully, am i right? he is using some arbitrary value of index for key as well as for onClick argument. Commented Mar 23, 2019 at 14:13
  • You have defined your rowIndex and colIndex outside the forEach respectively and in each iteration you are increasing them by 1. So in your function enableEdit rowIndex and colIndex are both passed as 3. The console logs show correct value because they were printed during the iteration and same goes for keys. Commented Mar 23, 2019 at 14:31
  • @UtsavPatel Oh, now i got it. onClick event isn't attached to it on every iteration right? Commented Mar 23, 2019 at 14:33
  • 1
    Glad you got it ! Commented Mar 23, 2019 at 14:39

1 Answer 1

1

UtsavPatel helped me much to understand what happened. After ForEach iteration rowIndex and colIndex values are 3, 3. We can even see that if we console.log after loop.

So what i did is just create a new object and store data that won't be touched there, like this:

    rowValues.forEach(colVal => {
      const indexDat = { rowIndex, colIndex };
      rowData.push(
        <td key={colIndex} onClick={() => this.enableEdit(indexDat)}>
          {colVal}
        </td>
      );
      console.log("row: " + rowIndex + ", column: " + colIndex);
      colIndex++;
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.