ReactJS creating trouble with incorporation of CSS in Table

I've noticed some strange behavior with the alignment of my table data. It seems to be all over the place. How can I fix this issue? Is there a way to specify column widths and ensure the text starts from the same position?

Take a look at the table below:

table

Below is the code snippet that contains the table header:

    const HospitalsList = (props) => (
<div className="content-container">
    <div className="header__content">
        <h1>Hospitals List</h1>
    </div>
    
    <HospitalsListFilters />

    <AddHospitalPage/>
        <table className="content-table">
            <thead>
               
                <tr>
                    <th>Hospital Name</th>
                    <th>Province</th>
                    <th>Sector</th>
                    <th>No of OR</th>
                    <th>No of Beds</th>
                </tr>

             </thead>
        </table>
    {props.hospitals.map((hospital) => {
       return <HospitalListItem key={hospital.id}{...hospital} />
    })}

</div>
)

And here is the code for the table data:

const HospitalListItem =({id, name, province, sector, noOfOR, noOfBeds,lastUpdate, dispatch}) => (
<div>
    <table className="content-table">
        <tbody>
            <tr>
            
                <Link to ={`/edit/${id}`}>
                    <td>{name}</td>
                </Link>
                <td>{province}</td>
                <td>{sector}</td>
                <td>{noOfOR}</td>
                <td>{noOfBeds}</td>
            </tr>
        </tbody>
    </table>
</div>

Here is the CSS file styling:

    .content-table {
    border-collapse: collapse;
    font-size: larger;
    min-width: 1200px;
    max-width: 1400px;
    border-radius: 5px 5px 0 0;
    overflow: hidden;
    box-shadow: 0 0 20px rgba(0,0,0,0.15);
    margin-left: $l-size;
  
    thead tr {
        th{
            padding : 12px 15px;

        }
     
        background-color: teal;
        color: white;
        font-weight: bold;
        
    }
    td {
        padding : 12px;
        

    }
    tbody tr {
        border-bottom: 1px solid #dddddd;
    }
    tbody tr:last-of-type{
        border-bottom: 2px solid #dddddd;
    }

}

Answer №1

When creating tables, it's important to avoid creating a new table for every row. Instead, utilize the .map function within the main table body (tbody):

<table className="content-table">
  <thead>
    <tr>
      <th>Hospital Name</th>
      <th>Province</th>
      <th>Sector</th>
      <th>No of OR</th>
      <th>No of Beds</th>
    </tr>
  </thead>
  <tbody>
    {props.hospitals.map((hospital) => {
       return <HospitalListItem key={hospital.id} {...hospital} />
    })}
  </tbody>
</table> 

Rather than rendering a full table in HospitalListItem, simplify it to render only a table row:

const HospitalListItem =({id, name, province, sector, noOfOR, noOfBeds,lastUpdate, dispatch}) => (
  <tr>
    <td><Link to={`/edit/${id}`}>{name}</Link></td>
    <td>{province}</td>
    <td>{sector}</td>
    <td>{noOfOR}</td>
    <td>{noOfBeds}</td>
  </tr>
)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

React: Transmitting information from a parent to a child component

Working on a React app, I find myself in a situation where I need to update the parent's state from a child component. Instead of following the common practice of passing a function as a prop to the child, I decided to take a different approach by pas ...

Could anyone provide a detailed explanation of how model binding functions in ASP.NET?

Hey there, I've been working on sending a post request from Reactjs to an ASP.NET API using axios. Initially, I encountered a 400 Bad request error. However, after making some changes to the DTO in the backend, the request went through. But now, the d ...

Converting dynamic content within a div into an interactive link

I am currently working with Longtail's JW Player and facing some difficulties with a basic function. Since I am not familiar with the programming language terminologies, I will describe the issue step by step: There is a JavaScript code that displays ...

Autofill functions may not be compatible with input fields generated using JavaScript

Having trouble with browsers not using autocomplete in login overlays generated with JavaScript? It can be really annoying. Any suggestions on how to fix this issue? Should I create a hidden form within the original HTML and then move it into the overlay? ...

Dealing with Click Events Outside of a React Component in TypeScript

For my first time using react typescript, I implemented an outside click event. However, I encountered a problem where clicking on the active or selected component triggers both the onclick and outside click events simultaneously. Here is a GIF link to he ...

Guide to incorporating animated material icons in vuetify

in Vuetify makes it easy to utilize mainstream material design icons through the use of the v-icon component. An example of this would be: <v-icon>home</v-icon> I am curious if animated material icons are supported and can be integrated in ...

React Native's setState function not causing a re-render

When using this component, the expected behavior is as follows: I press it, the selectedOpacity() function is called, the state is updated so it now renders with opacity=1. However, for some reason, after calling this.setState, it is not re-rendering. I h ...

Is there a way to create a mobile-exclusive slideshow using JavaScript?

I am trying to create a slideshow on my website, but whenever I add JavaScript it seems to break the desktop version. I want these three pictures to remain static and only start sliding when viewed on a mobile device. I have searched for resources on how t ...

Production servers encountering issues with Material-ui Official SSR example

After deploying the official material-ui ssr example to Microsoft Azure and Heroku, I encountered issues with it not working properly. Surprisingly, everything appears to be fine when running it on my local machine or using codesandbox. I'm puzzled as ...

Footer not sticking to bottom using bootstrap version 5.2.2

I am currently working on a project using Laravel Inertia Vue and bootstrap 5.2.2 as part of my dependencies. After loading the css to the tag, I attempted to create a sticky footer. However, I found that the footer is not sticking to the bottom as expect ...

Conceal the server's internal file structure to prevent it from being displayed in sources

When I visit my website and inspect the elements in Google Chrome, I am able to see a list of all the folders on my site under the sources tab. This reveals their original names and allows for the downloading of the entire website code. I am concerned abo ...

Automating the box selection process using table-react functionality

I am facing an issue with table-react. I need to implement a functionality where certain checkboxes should be checked based on user permissions. For instance, if the user has an id = 3 and can view companies with ids: 5, 6, 7, the checkboxes corresponding ...

Tips for changing the value of a TextField in React Material

I am faced with a challenge regarding a form that is populated with data from a specific country. This data is fetched from a GraphQL API using Apollo Client. By default, the data in the form is read-only. To make changes, the user needs to click on the e ...

Customize your Material UI theme colors with SASS variables

I am exploring the idea of customizing the theme of Material UI in a dynamic way by incorporating SASS Variables that would update the Material UI theme automatically. When designing my page with Sass, I typically use variables to define colors: $primary ...

Sharing application state between different routes in React Router v4 can be achieved by using methods such

I'm seeking solutions to address the following challenge: Without utilizing any state management library, I solely rely on React. The primary application state is contained within the <MyFoodApp/> component, which includes an array of restaura ...

Navigate to the chosen item in material-ui scroll bar

Currently, I have a list created using material-ui which contains numerous items and displays a scrollbar due to its size. I am looking for a way to automatically scroll to the selected item within the list. Does anyone have any suggestions on how I can a ...

Is there a way to directly access the React component that was clicked?

I'm looking to dynamically change the class of a component when clicked. By using state to create a new component with properties such as name and done (initiated as false), which are then added to the todos array, I want to find out how to identify t ...

A useful tip for ensuring that `import` always reads files from the `src/` directory in a Reactjs project

After inheriting a React project that worked on my previous computer, I encountered difficulties building it on my new computer. The issue seems to be with the npm start command not recognizing the project directory. For instance, consider these two files ...

What causes a blank screen to appear instead of an error message when something goes wrong?

Whenever a Javascript error occurs, my screen goes blank. Is there a way to make React display error messages directly on the browser window? ...

Styling Angular2 Material Dialog: the perfect fit

The Angular2 material team recently unveiled the MDDialog module at https://github.com/angular/material2/blob/master/src/lib/dialog/README.md I am interested in customizing the appearance of Angular2 material's dialog. Specifically, I want to adjust ...