Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js- using html to make grid dynamically

Currently I am working on one of FCC's project, Game of Life

Before getting started, I am working on simply how to render the grid onto the page.

I want to be able to change the dimension of the table while still keeping in retain inside the container that the table it's in.

I am using Materialize.css as CSS Framework.

Component:

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 what it's being 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 am having right now is that although I can change the dimension of the table at ease (by changing the state.width and state.height in React), it goes overflow the container.

In other words, if I want to set the container at a fixed length and width and if I set the dimension of table at a relative high number, it will overflow.

Is there way I can override this? Or is it using table is not a good option to achieve this goal?

like image 667
Alejandro Avatar asked Nov 24 '25 17:11

Alejandro


1 Answers

You should use css3 Flexible Box (flexbox) layout.

  • https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Using_CSS_flexible_boxes
  • http://learnlayout.com/flexbox.html
like image 166
juanmaia Avatar answered Nov 27 '25 07:11

juanmaia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!