0

I receive a JSON object back from my DB

0: {id: 8364, tableId: 137, rowIndex: 0, columnIndex: 0, content: "2015 "}
1: {id: 835, tableId: 137, rowIndex: 0, columnIndex: 1, content: "2016"}
2: {id: 836, tableId: 137, rowIndex: 0, columnIndex: 2, content:"2018"}
3: {id: 837, tableId: 137, rowIndex: 0, columnIndex: 3, content:"2017"}
4: {id: 838, tableId: 137, rowIndex: 0, columnIndex: 4, content:"Change"}
5: {id: 839, tableId: 137, rowIndex: 0, columnIndex: 5, content:"Profit"}
6: {id: 830, tableId: 137, rowIndex: 1, columnIndex: 0, content: "Cash Summary"}
7: {id: 831, tableId: 137, rowIndex: 1, columnIndex: 1, content: "$1200"}

I want to create a HTML table using the data returned. The data is saved in const sortedData How can I use the map function to use the rowIndex and columnIndex to render a table into a react component

In this case the table is 6X6, how do I dynamically set the elements in the even a different size table is returned

Currently I have

    let table = sortedData.map((item, i) => {
      return (
        <table>
          <tr key={i} value={item}>
            <td>{item.content}</td>
            <td>{item.content}</td>
            <td>{item.content}</td>
            <td>{item.content}</td>
            <td>{item.content}</td>
          </tr>
        </table>

      );
    });

2
  • Can you please show your effort what you've tried so far? Commented Aug 12, 2019 at 8:05
  • Do you can to show tr and td based on row index and column index? Commented Aug 12, 2019 at 8:15

2 Answers 2

1

You want to create an array of arrays [[rowIndex1Item1, rowIndex2Item2], [rowIndex2Item1]], categorized by rowIndex. Each inner-array will be a table-row and their items will be table-cells.

See working sandbox: https://codesandbox.io/s/cool-bush-wpn4m

Try something like below:

import React from "react";
import ReactDOM from "react-dom";

import "./styles.css";

const data = [
  { id: 834, tableId: 137, rowIndex: 0, columnIndex: 0, content: "2015" },
  { id: 835, tableId: 137, rowIndex: 0, columnIndex: 1, content: "2016" },
  { id: 836, tableId: 137, rowIndex: 0, columnIndex: 2, content: "2018" },
  { id: 837, tableId: 137, rowIndex: 0, columnIndex: 3, content: "2017" },
  { id: 838, tableId: 137, rowIndex: 0, columnIndex: 4, content: "Change" },
  { id: 839, tableId: 137, rowIndex: 0, columnIndex: 5, content: "Profit" },
  {
    id: 830,
    tableId: 137,
    rowIndex: 1,
    columnIndex: 0,
    content: "Cash Summary"
  },
  { id: 831, tableId: 137, rowIndex: 1, columnIndex: 1, content: "$1200" }
];

const createTable = () => {
  const organizedByRows = data.reduce((obj, curr) => {
    if (!obj[curr.rowIndex]) {
      obj[curr.rowIndex] = [];
    }

    obj[curr.rowIndex].push(curr);
    return obj;
  }, {});

  const tableContent = Object.values(organizedByRows).map(row => {
    return (
      <tr>
        {row.map(item => (
          <td>{item.content}</td>
        ))}
      </tr>
    );
  });

  return <table>{tableContent}</table>;
};

const App = () => {
  return <div>{createTable()}</div>;
};

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Sign up to request clarification or add additional context in comments.

1 Comment

It wont work for different table id , you need to create seperate table when tableId changes
0

I think what you are doing is mostly correct except that you have to move the <table> element out of map like below.

<table>
    {sortedData.map((item, i) => {
        return <tr key={i} value={item}>
            <td>{item.content}</td>
            <td>{item.content}</td>
            <td>{item.content}</td>
            <td>{item.content}</td>
            <td>{item.content}</td>
        </tr>;
        })
    }
</table>;

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.