React.js - Dynamically create grid with HTML


Alejandro

Currently, I'm working on an FCC project called "The Game of Life"

Before starting, I was simply working on how to render the grid onto the page.

I want to be able to change the dimensions of the table while still staying in the container the table is in.

I am using Materialize.css as CSS framework.

Components:

import React, { Component } from 'react'

export default class Example extends Component {
  constructor(props){
    super(props);
    this.state = {height: 3, width: 3}
  }
  render(){
    let rows = [];
    for (var i = 0; i < this.state.height; i++){
      let rowID = `row${i}`
      let cell = []
      for (var idx = 0; idx < this.state.width; idx++){
        let cellID = `cell${i}-${idx}`
        cell.push(<td key={cellID} id={cellID}></td>)
      }
      rows.push(<tr key={i} id={rowID}>{cell}</tr>)
    }
    return(
      <div className="container">
        <div className="row">
          <div className="col s12 board"></div>
            <table id="simple-board">
               <tbody>
                 {rows}
               </tbody>
             </table>
          </div>
      </div>
    )
  }
}

This is rendered from React:

  <div className="container">
    <div className="row">
      <div className="col s12 board"></div>
        <table id="simple-board">
          <tbody>
            <tr id="row0">
              <td id="cell0-0"></td>
              <td id="cell0-1"></td>
              <td id="cell0-2"></td>
            </tr>
            <tr id="row1">
              <td id="cell1-0"></td>
              <td id="cell1-1"></td>
              <td id="cell1-2"></td>
            </tr>
            <tr id="row2">
              <td id="cell2-0"></td>
              <td id="cell2-1"></td>
              <td id="cell2-2"></td>
            </tr>
          </tbody>
        </table>
      </div>
  </div>

CSS

table {
  width: 100%;
  table-layout: fixed;
  overflow: hidden;
}

td{
  width: 33.33%;
  position: relative;
  color: #101935;
  background: #F2FDFF;
  border: 4px solid #DBCBD8;
  border-radius: 12px;
  cursor: pointer;
  transition: background 0.5s ease-out, color 0.5s ease-out;
}

td:hover{
  background: #564787;
}

The problem I'm having now is that although I can easily change the dimensions of the table (by changing state.width and state.height in React), it overflows the container.

In other words, if I wanted to set the container to a fixed length and width, and set the table's dimensions to a relatively high number, it would overflow.

Is there any way to override this? Or is using a table not a good option to achieve this?

Related


React.js - Dynamically create grid with HTML

Alejandro Currently, I'm working on an FCC project called "The Game of Life" Before starting, I was simply working on how to render the grid onto the page. I want to be able to change the dimensions of the table while still staying in the container the table i

React.js - Dynamically create grid with HTML

Alejandro Currently, I'm working on an FCC project called "The Game of Life" Before starting, I was simply working on how to render the grid onto the page. I want to be able to change the dimensions of the table while still staying in the container the table i

React.js - Dynamically create grid with HTML

Alejandro Currently, I'm working on an FCC project called "The Game of Life" Before starting, I was simply working on how to render the grid onto the page. I want to be able to change the dimensions of the table while still staying in the container the table i

React.js - Dynamically create grid with HTML

Alejandro Currently, I'm working on an FCC project called "The Game of Life" Before starting, I was simply working on how to render the grid onto the page. I want to be able to change the dimensions of the table while still staying in the container the table i

Dynamically create Bootstrap grid

Swopnilnep I'm trying to dynamically generate bootstrap columns using a javascript function. The purpose is to make them appear as different columns in the same row. It currently looks like this: I call this function on window.onload() with the div id: // Upda

Dynamically create Bootstrap grid

Swopnilnep I'm trying to dynamically generate bootstrap columns using a javascript function. The purpose is to make them appear as different columns in the same row. It currently looks like this: I call this function on window.onload() with the div id: // Upda

Dynamically create Bootstrap grid

Swopnilnep I'm trying to dynamically generate bootstrap columns using a javascript function. The purpose is to make them appear as different columns in the same row. It currently looks like this: I call this function on window.onload() with the div id: // Upda

Dynamically create Bootstrap grid

Swopnilnep I'm trying to dynamically generate bootstrap columns using a javascript function. The purpose is to make them appear as different columns in the same row. It currently looks like this: I call this function on window.onload() with the div id: // Upda

Dynamically create complex HTML grid given a list of shapes

Four_lo Input: I start with a list of squares and rectangles. There are 4 possible sizes (1x1, 1x2, 2x1 and 2x2). The list can be of any length or any combination of the 4 shapes. Problem: I want to stack all these shapes into a container of width 4 with no de

Dynamically create html grid for rows and columns using python

Matt I am trying to create a grid of rows and columns using python. Basically, for the first element in the userName list, I should create a <div class="row">loop, and then ideally, for every fourth iteration of the loop, I should close the row with another di

Dynamically create complex HTML grid given a list of shapes

Four_lo Input: I start with a list of squares and rectangles. There are 4 possible sizes (1x1, 1x2, 2x1 and 2x2). The list can be of any length or any combination of the 4 shapes. Problem: I want to stack all these shapes into a container of width 4 with no de

Dynamically create complex HTML grid given a list of shapes

Four_lo Input: I start with a list of squares and rectangles. There are 4 possible sizes (1x1, 1x2, 2x1 and 2x2). The list can be of any length or any combination of the 4 shapes. Problem: I want to stack all these shapes into a container of width 4 with no de

Dynamically create html grid for rows and columns using python

Matt I am trying to create a grid of rows and columns using python. Basically, for the first element in the userName list, I should create a <div class="row">loop, and then ideally, for every fourth iteration of the loop, I should close the row with another di

Dynamically create HTML table in Express.js

Joao Pimentel Ferreira I'm new to Node.js/Express.js and have read a lot, but I admit, maybe I'm still stuck with the PHP paradigm. I'm thinking of moving my site's server from PHP to Express.js, but some issues are still not working out. For example, in PHP,

Dynamically create HTML table in Express.js

Joao Pimentel Ferreira I'm new to Node.js/Express.js and have read a lot, but I admit, maybe I'm still stuck with the PHP paradigm. I'm thinking of moving my site's server from PHP to Express.js, but some issues are still not working out. For example, in PHP,

How to create these HTML elements dynamically using JS

Luis Adorno I want to create this layout, dynamically create its list, fetch data from database and populate it. Here is the code for the layout: <div class="card-body"> <!-- person --> <div class="row" id="img_div"> <div class="col-12 col-sm-12 col-md

Dynamically create HTML table in Express.js

Joao Pimentel Ferreira I'm new to Node.js/Express.js and have read a lot, but I admit, maybe I'm still stuck with the PHP paradigm. I'm thinking of moving my site's server from PHP to Express.js, but some issues are still not working out. For example, in PHP,

Dynamically create HTML table in Express.js

Joao Pimentel Ferreira I'm new to Node.js/Express.js and have read a lot, but I admit, maybe I'm still stuck with the PHP paradigm. I'm thinking of moving my site's server from PHP to Express.js, but some issues are still not working out. For example, in PHP,

How to create a grid dynamically in unity

Samsung My vertices have color values. I want to make a mesh using vertices with the same color value. This photo is an example. I took a picture with an android phone and did an image segmentation of the object So I get a color value corresponding to the coor

How to create a dynamically changing grid?

Uncle Bear What I'm trying to say at the beginning is that I'm just starting my adventures with Xaml, so please understand if the question is trivial. I'm going to design a user control with variable layout, which is controlled by radio buttons in a UWP - C++C

Java dynamically create button in grid

Hassett I am creating a desktop application in Java in which I want to create buttons dynamically. Dynamically created buttons should be placed in a grid-like structure. Now, my concern is, if I want to access these buttons, what should I do since I don't have

Java dynamically create button in grid

Hassett I am creating a desktop application in Java in which I want to create buttons dynamically. Dynamically created buttons should be placed in a grid-like structure. Now, my concern is, if I want to access these buttons, what should I do since I don't have

Can we create links dynamically using React JS?

use Imagine I want to create a web application like Instagram with React where users can add pictures. Then, associate each image you add to a URL, for example: https://www.instagram.com/p/BTg4ZopggE9 what should we do? I know there is a module "react-router",

Can we create links dynamically using React JS?

use Imagine I want to create a web application like Instagram with React where users can add pictures. Then, associate each image you add to a URL, for example: https://www.instagram.com/p/BTg4ZopggE9 what should we do? I know there is a module "react-router",

Can we create links dynamically using React JS?

use Imagine I want to create a web application like Instagram with React where users can add pictures. Then, associate each image you add to a URL, for example: https://www.instagram.com/p/BTg4ZopggE9 what should we do? I know there is a module "react-router",