Implementing Dynamic Table in React

share link

by Abdul Rawoof A R dot icon Updated: Feb 10, 2023

technology logo
technology logo

Solution Kit Solution Kit  

Dynamic tables change their shape based on the addition or deletion of input. We can create a dynamic table in react by using JSON. It will be an easy approach as the table will dynamically add rows and columns in addition to JSON object data, so we do not need to add static rows and columns. We must follow a simple algorithm for creating the dynamic table: 

  • Create a JSON array of data 
  • Create a Dynamic Table Component 
  • Render Dynamic Table Component. This can be done by using the render function. 
  • render(): It is used to redirect a page to some other page. It provides help in displaying specific views in the UI by using specific render function logic and returning the result. 


Dynamic Tables can be used to maintain records of students in a school. Whenever a new student is admitted, a new JSON object data will be created and added to the table. 


For more information about implementing a dynamical table in React, refer to the code given below 

Fig : Preview of the output that you will get on running this code from your IDE.

Code

In this solution we are using React library.

import React from 'react';

const Tab = () => {

    const schoolTimings = [
        {
            schoolType: 'Primary School',
            list: [
                {
                    school: 'ABC',
                    day: 'Monday',
                    open: '08:00',
                    close: '12:30'
                },
                {
                    school: 'ABC',
                    day: 'Tuesday',
                    open: '08:00',
                    close: '12:30'
                },
                {
                    school: 'ABC',
                    day: 'Thursday',
                    open: '08:00',
                    close: '12:30'
                },
                {
                    school: 'ABC',
                    day: 'Friday',
                    open: '08:00',
                    close: '12:30'
                },
                {
                    school: 'ABC',
                    day: 'Saturday',
                    open: '08:00',
                    close: '12:30'
                },
                {
                    school: 'XYZ',
                    day: 'Monday',
                    open: '08:00',
                    close: '12:00'
                },
                {
                    school: 'XYZ',
                    day: 'Tuesday',
                    open: '08:00',
                    close: '12:00'
                },
                {
                    school: 'XYZ',
                    day: 'Wednesday',
                    open: '08:00',
                    close: '12:00'
                },
                {
                    school: 'XYZ',
                    day: 'Thursday',
                    open: '08:00',
                    close: '12:00'
                },
                {
                    school: 'XYZ',
                    day: 'Friday',
                    open: '08:00',
                    close: '12:00'
                },
                {
                    school: 'XYZ',
                    day: 'Saturday',
                    open: '08:00',
                    close: '12:00'
                }
            ]
        },
        {
            schoolType: 'Secondary School',
            list: [
                {
                    school: 'PQR',
                    day: 'Monday',
                    open: '08:00',
                    close: '15:30'
                },
                {
                    school: 'PQR',
                    day: 'Tuesday',
                    open: '08:00',
                    close: '15:30'
                },
                {
                    school: 'PQR',
                    day: 'Wednesday',
                    open: '08:00',
                    close: '15:30'
                },
                {
                    school: 'PQR',
                    day: 'Thursday',
                    open: '08:00',
                    close: '15:30'
                },
                {
                    school: 'PQR',
                    day: 'Friday',
                    open: '08:00',
                    close: '15:30'
                },
                {
                    school: 'PQR',
                    day: 'Saturday',
                    open: '08:00',
                    close: '15:30'
                },
                {
                    school: 'XYZ',
                    day: 'Monday',
                    open: '08:00',
                    close: '17:00'
                },
                {
                    school: 'XYZ',
                    day: 'Tuesday',
                    open: '08:00',
                    close: '17:00'
                },
                {
                    school: 'XYZ',
                    day: 'Wednesday',
                    open: '08:00',
                    close: '17:00'
                },
                {
                    school: 'XYZ',
                    day: 'Thursday',
                    open: '08:00',
                    close: '17:00'
                },
                {
                    school: 'XYZ',
                    day: 'Friday',
                    open: '08:00',
                    close: '17:00'
                },
                {
                    school: 'XYZ',
                    day: 'Saturday',
                    open: '08:00',
                    close: '17:00'
                }
            ]
        }
    ];

    let table2 = <></>
    for (let i = 0; i < schoolTimings.length; i++) {
        table2 = <>{table2}
            <tbody>
            <tr>
                <td colSpan="15"  style={{color: "blue", fontWeight: "bold"}}>  {schoolTimings[i].schoolType}  </td>
            </tr>
            </tbody>
        </>;
        let listSchool = []
        for (let j = 0; j < schoolTimings[i].list.length; j++) {
            if (listSchool.indexOf(schoolTimings[i].list[j].school) === -1) {
                listSchool.push(schoolTimings[i].list[j].school)
            }
        }
        for (let k = 0; k < listSchool.length; k++) {
            let listValueSchool = {
                School: listSchool[k],
                Monday: {
                    open: '',
                    close: ''
                },
                Tuesday: {
                    open: '',
                    close: ''
                }, Wednesday: {
                    open: '',
                    close: ''
                }, Thursday: {
                    open: '',
                    close: ''
                }, Friday: {
                    open: '',
                    close: ''
                }, Saturday: {
                    open: '',
                    close: ''
                }, Sunday: {
                    open: '',
                    close: ''
                },
            }
            for (let j = 0; j < schoolTimings[i].list.length; j++) {
                if (schoolTimings[i].list[j].school === listSchool[k]) {
                    switch (schoolTimings[i].list[j].day) {
                        case 'Monday':
                            listValueSchool.Monday = {
                                open: schoolTimings[i].list[j].open,
                                close: schoolTimings[i].list[j].close
                            }
                            break;
                        case 'Tuesday':
                            listValueSchool.Tuesday = {
                                open: schoolTimings[i].list[j].open,
                                close: schoolTimings[i].list[j].close
                            }
                            break;
                        case 'Wednesday':
                            listValueSchool.Wednesday = {
                                open: schoolTimings[i].list[j].open,
                                close: schoolTimings[i].list[j].close
                            }
                            break;
                        case 'Thursday':
                            listValueSchool.Thursday = {
                                open: schoolTimings[i].list[j].open,
                                close: schoolTimings[i].list[j].close
                            }
                            break;
                        case 'Friday':
                            listValueSchool.Friday = {
                                open: schoolTimings[i].list[j].open,
                                close: schoolTimings[i].list[j].close
                            }
                            break;
                        case 'Saturday':
                            listValueSchool.Saturday = {
                                open: schoolTimings[i].list[j].open,
                                close: schoolTimings[i].list[j].close
                            }
                            break;
                        case 'Sunday':
                            listValueSchool.Sunday = {
                                open: schoolTimings[i].list[j].open,
                                close: schoolTimings[i].list[j].close
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            table2 = <>{table2}
                <tbody>
                <tr>
                    <td>{listValueSchool.School}</td>
                    <td>{listValueSchool.Sunday.open}</td>
                    <td>{listValueSchool.Sunday.close}</td>
                    <td>{listValueSchool.Monday.open}</td>
                    <td>{listValueSchool.Monday.close}</td>
                    <td>{listValueSchool.Tuesday.open}</td>
                    <td>{listValueSchool.Tuesday.close}</td>
                    <td>{listValueSchool.Wednesday.open}</td>
                    <td>{listValueSchool.Wednesday.close}</td>
                    <td>{listValueSchool.Thursday.open}</td>
                    <td>{listValueSchool.Thursday.close}</td>
                    <td>{listValueSchool.Friday.open}</td>
                    <td>{listValueSchool.Friday.close}</td>
                    <td>{listValueSchool.Saturday.open}</td>
                    <td>{listValueSchool.Saturday.close}</td>

                </tr>
                </tbody>
            </>;
        }


    }


    return (
        <table>
            <thead>
            <tr>

                <th colSpan="1" style={{color: "purple"}}>Day</th>
                <th colSpan="2" style={{color: "purple"}}>Sunday</th>
                <th colSpan="2" style={{color: "purple"}}>Monday</th>
                <th colSpan="2" style={{color: "purple"}}>Tuesday</th>
                <th colSpan="2" style={{color: "purple"}}>Wednesday</th>
                <th colSpan="2" style={{color: "purple"}}>Thursday</th>
                <th colSpan="2" style={{color: "purple"}}>Friday</th>
                <th colSpan="2" style={{color: "purple"}}>Saturday</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td colSpan="1">School</td>
                <td colSpan="1">Open</td>
                <td colSpan="1">Close</td>
                <td colSpan="1">Open</td>
                <td colSpan="1">Close</td>
                <td colSpan="1">Open</td>
                <td colSpan="1">Close</td>
                <td colSpan="1">Open</td>
                <td colSpan="1">Close</td>
                <td colSpan="1">Open</td>
                <td colSpan="1">Close</td>
                <td colSpan="1">Open</td>
                <td colSpan="1">Close</td>
                <td colSpan="1">Open</td>
                <td colSpan="1">Close</td>
            </tr>
            </tbody>
            {table2}
        </table>
    );
}

export default Tab;

Instructions

Follow the steps carefully to get the output easily.

  1. Install the Node.js and React on your IDE(preferable Visual Studio Code).
  2. Create React Application using npx create-react-app foldername.
  3. cd foldername.
  4. Open the folder in IDE.
  5. Copy the code using "copy" button above and paste it in app.js file(remove the earlier code from app.js).
  6. Open the terminal from IDE.
  7. npm start to run the file.


I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.


I found this code snippet by searching for 'how to implement dynamic table in react' in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in Visual Studio Code 1.73.1.
  2. The solution is tested on node v18.12.1 and npm v8.19.2.
  3. React version-18.2.0.


Using this solution, we are able to implement dynamic table in react with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to implement dynamic table in react.

Dependent Library

reactby facebook

JavaScript doticonstar image 209050 doticonVersion:v18.2.0doticon
License: Permissive (MIT)

The library for web and native user interfaces

Support
    Quality
      Security
        License
          Reuse

            reactby facebook

            JavaScript doticon star image 209050 doticonVersion:v18.2.0doticon License: Permissive (MIT)

            The library for web and native user interfaces
            Support
              Quality
                Security
                  License
                    Reuse

                      You can also search for any dependent libraries on kandi like 'react'.

                      Support

                      1. For any support on kandi solution kits, please use the chat
                      2. For further learning resources, visit the Open Weaver Community learning page.


                      See similar Kits and Libraries