2

Is it possible to render dynamic columns and also data on column bases

I'm trying to render dynamic table columns and also its data on the basis of array in react JS.

var cols = ["Id", "Name"];
const generateData = () => {
    var testArr = [];
    for (let i = 0; i < 100; i++) {
        testArr.push({ Id: i + 1, Name: names[Math.round(Math.random() * 10)] });
    }
    return testArr;
}

import { Table } from "reactstrap";

function dynamicTables(props) {
    debugger
    return (
        <Table className="align-items-center table-flush" responsive>
            <thead className="thead-light">
                <tr>
                    {
                        props.cols.map((item, key) => (
                            <th key={key}>{item}</th>
                        ))
                    }
                </tr>
            </thead>
            <tbody>
                {
                    props.data.map((item, key) => (
                        <tr key={key}>
                            {
                                props.cols.map((col) => {
                                    <td>{item[col]}</td>
                                })
                            }
                        </tr>
                    ))
                }
            </tbody>
        </Table>
    );
}

export default dynamicTables;

Calling Component:

<dynamicTables data={generateData()} cols={cols}></dynamicTables>
4
  • Have you tried? Commented Dec 19, 2022 at 11:53
  • Yes, I have try Commented Dec 20, 2022 at 3:47
  • And is it possible? Commented Dec 20, 2022 at 8:30
  • 1
    Yes it is possible and I resolved it. Thanks @Konrad Commented Dec 22, 2022 at 11:22

1 Answer 1

1

This worked for me

var cols = ["Id", "Name"];
const generateData = () => {
    var testArr = [];
    for (let i = 0; i < 100; i++) {
        testArr.push({ Id: i + 1, Name: names[Math.round(Math.random() * 10)] });
    }
    return testArr;
}

import { Table } from "reactstrap";

function dynamicTables(props) {
    debugger
    return (
        <Table className="align-items-center table-flush" responsive>
            <thead className="thead-light">
                <tr>
                    {
                        props.cols.map((item, key) => (
                            <th key={key}>{item}</th>
                        ))
                    }
                </tr>
            </thead>
            <tbody>
                {
                    props.data.map((item, key) => (
                        <tr key={key}>
                            {
                                props.cols.map((col) => {
                                    return <td>{item[col]}</td>
                                })
                            }
                        </tr>
                    ))
                }
            </tbody>
        </Table>
    );
}

export default dynamicTables;
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.