1

I am beginner in react js, this might sound foolish question, but i am not able to create this empty table based on number of row and columns entered in input component. and then fill those cell with some data and store it into variable. here is something that i have tried but doesn't meet the expectation,as i am not able to fetch data from these table cells https://codesandbox.io/s/brave-noether-zukhy?file=/demo.js

1 Answer 1

1

STEP -1 : first try to maintain a separate state for each of the table cells

 const [tableCellsData, setTableCellsData] = useState({});

STEP -2 : try to have a unique index from the position of the array . i implemented like this

  const getUniqueKeyFromArrayIndex = (rowNum, columnNum) => {
    return `${rowNum}-${columnNum}`;
  };

STEP -3 : attatch it to every input boxes by using the name property

<InputBase
      name={getUniqueKeyFromArrayIndex(i, j)}
      defaultValue="TextInput"
      Value={data}
      onChange={onChangeHandler}
/>

STEP -4 : pass your onChange handler function

const onChangeHandler = (e) => {
    console.log(e.target.name, e.target.value);
    setTableCellsData({
      ...tableCellsData,
      [e.target.name]: e.target.value
    });
  };

STEP -5 : you can check if everything is working as your expectation like this

  useEffect(() => {
    const keys = Object.keys(tableCellsData);
    keys.forEach((item) => {
      const indexes = item.split("-");
      console.log(
        `value of the item in index ${indexes} is  `,
        tableCellsData[item]
      );
    });
  }, [tableCellsData]);

here is the full example https://codesandbox.io/s/naughty-dawn-clrl2?file=/demo.js

Sign up to request clarification or add additional context in comments.

1 Comment

how can we have list of dictionary values as it is table, instead of just dictionary with index and value

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.