Home > Mobile >  How can I make div cells ordered correctly?
How can I make div cells ordered correctly?

Time:10-17

I want to make an app for creating seating charts.
However, the problem is, I can't make the seats made with Div ordered correctly. It looked better before I added the margin attribute to the Seat class, but once I did it, the seats started to separate.

Here are the codes related to seats and their container:

class App {
    constructor() {
        this.seatMap = new SeatMap(document.getElementById("SeatMapContainer"));
        this.seatMap.createSeats(6,6);
    }
}

class SeatMap {
    constructor(parentElement) {
        this.parentElement = parentElement;
        this.body = document.createElement("div");
        this.body.className = "SeatMap";
        this.parentElement.appendChild(this.body);
        this.seatList = [];
    }

    createSeats(row, col) {
        this.seatList = [];
        for (var a = 0; a < row; a  ) {
            this.seatList.push([]);
            for (var b = 0; b < col; b  ) {
                this.seatList[a].push(new Seat(this.body));
            }
        }
        
    }
}

class Seat {
    constructor(seatContainer) {
        // color, name, and so on.
        this.seatContainer = seatContainer;
        this.seatBody = document.createElement("div");
        this.seatBody.className = "Seat";
        this.seatContainer.appendChild(this.seatBody);
        console.log("seatcreated");
    }
}

var main = new App();
#Display {
    position: absolute;
    top: 50px;
    left: 50px;
    width: calc(100vw - 50px);
    height: calc(100% - 50px);
    border: 1px solid black;
    box-sizing: border-box;
    background-color: white;
    z-index: 97;
}

.SeatMap {
    position: relative;
    width: 100%;
    height: 100%;
    margin: auto;
    margin-top: 50px;
    padding: 0;
    border: 1px solid black;
    columns: 6;
}

.Seat {
    position: relative;
    margin: 25px;
    width: 100px;
    height: 60px;
    border: 1px solid black;
}
<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf-8">
        <title>Demo</title>
    </head>
    <body>
        <div id="Display">
            <div id="SeatMapContainer"></div>
        </div>
    </body>
</html>

How can I solve this problem?

CodePudding user response:

const nColumns = 6
const nRows = 6

const seatMap = document.createElement('div')
seatMap.style.display = 'grid'
seatMap.style.gridTemplateColumns = `repeat(${nColumns}, 1fr)`
seatMap.style.gap = '10px'
document.body.appendChild(seatMap)

for (let i = 0; i < (nColumns*nRows); i  ) {
   const seat = document.createElement('div')
   seat.style.background = 'orange'
   seat.style.aspectRatio = '1/1'
   seat.innerText = i
   seatMap.appendChild(seat)
}

  • Related