html - 高度为 100% 的表格不适合父级大小

标签 html css html-table

我在主目录的 div 中有一个表。 所有这些都具有宽度 = 100%,但是当我使窗口变小时,只有 div Ant 主变得更小,但表格 dosent 调整大小。表格下方的所有其他元素都会更改位置和大小并开始覆盖它。该表有 25 条记录,如果窗口是全尺寸的,则一切都完美匹配。 我正在谈论的 div 有 id home

html:

<DOCTYPE! html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Admin Panel | Please Login</title>
    <link href='../css/admin.css' type='text/css' rel='stylesheet'>
    <meta http-equiv="refresh" content="60"> 
</head>
<body>
<main>
    <div id='container'>
        <img src="../img/ipw_quer_rgb.jpg" alt="IPW Logo" id="logo" width="15%">
        <header>

            <ul id='menu'>
            <div id="links">
                <li><a href="index.html">Home</a></li>
                <li><a href="/html/stmanager.html">Schüler verwalten</a></li>
                <li><a href="/html/history.html">History</a></li>
            </div>  
            </ul>

        </header>

            <div id="home">
                <h2 id="date"></h2>
                <table id="table">
                </table>
            </div>
            <div id="dropdown" class="dropdown">

                        <select id="select">
                          <option value="Nothing">Nichts ausgewählt</option>
                          <option value="Extern">Extern</option>
                          <option value="Termin">Termin</option>
                          <option value="Schule">Schule</option>
                        </select>   

            </div>

            <div id="legend" class="legend">
                        <svg width="10" height="10">
                        <rect x="0" y="0" width="10" height="10" style="fill:#D3D3D3;" />
                        </svg>
                        <a>Extern</a>
                        <br>
                        <svg width="10" height="10">
                        <rect x="0" y="0" width="10" height="10" style="fill:#FFAEB9;" />
                        </svg>
                        <a>Termin</a>
                        <br>
                        <svg width="10" height="10">
                        <rect x="0" y="0" width="10" height="10" style="fill:#FFFF00;" />
                        </svg>
                        <a>Schule</a>
                        <br>
                        <svg width="10" height="10">
                        <rect x="0" y="0" width="10" height="10" style="fill:#00FF00;" />
                        </svg>
                        <a>Visiert</a>
                        <br>
                        <button id="edit">Editieren</button>
            </div>
        </div>
    </main>

CSS:

*{
    margin: 0;
    padding: 0;
    font-family: monospace;
    box-sizing: border-box;
}




main{
    height: 100%;
    width: 100%;
}


label{
    font: 13px Helvetica, Arial, sans-serif;
}
html, body{
    background-color: #006975;
    overflow-y:auto;
    height: 100%;
    width: 100%;
    padding: 0;
    margin: 0;
}
h3{
    margin-right:50%;
}
table{
    width:100%;
    height:calc(100% -50px);
}

ul {
    width:100%;
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #006975;
}

li {
  width:25%;
  float: left;
}
h1{
    text-align:center;
}


li a {
  display: block;
  color: white;
  font-size: 120%;
  text-align: center;
  padding: 14px 16px;
  border-left: 2px solid #fff;
   border-right: 2px solid #fff;
  text-decoration: none;
}

/* Change the link color to #111 (black) on hover */
li a:hover {
    color: #006975;
    background-color: #fff;
}

button:hover{

    color:#fff;
    background-color:#84afb8;
}


#container{
    position:absolute;
    margin: 4% 4% 4% 4%;
    padding: 2%;
    width: 90%;
    height: 90%;
    background-color: #fff;
}


#home{
    height:60%;
}

#dropdown{
    width:100%;
    height:2%;
    visibility:hidden;
}

.cell {
    height: 4%;
    width:10%;
    text-align: center;
    background-color: #D3D3D3;
}

.cell.on {
height: 4%;
    width: 10%;
    background-color: #00FF00;
}
.cell.les {
height: 4%;
    width: 10%;
    background-color: #FFFF00;
}

.cell.term {
height: 4%;
    width: 10%;
    background-color: #FFAEB9;
}
.cell.ext{
height: 4%;
    width: 10%;
    background-color: #D3D3D3;
}

.cell.spacer {
height: 4%;
    width: 10%;
    background-color:white;
}

.name {
    border: 1px solid black;
}

如果你还需要javascript请询问

编辑: javascript:

getDataUser('logGLAUSB');

var names = ["Zebra","Benj", "Nico", "Timon","Miro", "Leo"];
var longpresstimer = null;

getData();
window.addEventListener('click', function(){
});


window.addEventListener('load', function () {

    var clickcount = 0;
    var singleClickTimer;
    document.getElementById('table').addEventListener('click', function (event) {

            clickcount++;
            if(clickcount==1){
                if(event.target.tagName != "INPUT" && event.target.classList != 'cell spacer'){
                    singleClickTimer = setTimeout(function() {
                        clickcount = 0;
                        var cell = event.target;
                        var selected = getSelected();
                        if(selected == 3){
                            cell.classList.remove("ext");
                            cell.classList.remove("term");
                            cell.classList.remove("les");
                            cell.classList.add("on");
                        }else{
                            cell.classList.remove("ext");
                            cell.classList.remove("term");
                            cell.classList.remove("les");
                            cell.classList.remove("on");
                            switch(selected){
                                case 0: cell.classList.add("ext"); break;
                                case 1: cell.classList.add("les"); break;
                                case 2: cell.classList.add("term"); break;
                            }
                        }
                        var x = "get";
                        x += getString(event.target.parentNode.cells[0].childNodes[0].innerHTML);
                        getData(x);

                    }, 300);
                }
            }else if (clickcount == 2){
                if(event.target.classList != "name"){
                    clearTimeout(singleClickTimer);
                    clickcount = 0;
                    toInput(event.target);
                }
        }
    });



    });


    document.getElementById("edit").addEventListener('click', function(){
        var legend = document.getElementById("legend");
        var dropdown = document.getElementById('dropdown');
        var select = document.getElementById('select');

            legend.style.visibility = 'hidden';
            dropdown.style.visibility = 'visible';
            var button = document.createElement('button');
            button.innerHTML= "Fertig";
            dropdown.appendChild(button);
            button.onclick = function(){
                legend.style.visibility = 'visible';
                dropdown.style.visibility = 'hidden';
                dropdown.removeChild(button);
                select.value = "Nothing";

            }


    });


    function reset(){
    var rows = Array.from(document.getElementsByClassName('row'));
                var table = document.getElementById('table');
                rows.forEach(function (row){
                    var cells = Array.from(document.getElementsByClassName('cell'));
                    for(var i = 0;i< cells.length;i++){
                    var cell = cells[i]
                        cell.classList.remove('on');
                        cell.classList.remove('les');
                        cell.classList.remove('term');  
                        cell.classList.remove('ext');   
                    }

                }); 
                var x = "rep";
                x += getResetString();
                getData(x);
    }

        function clearSelection() {
            if(document.selection && document.selection.empty) {
                document.selection.empty();
            } else if(window.getSelection) {
                var sel = window.getSelection();
                sel.removeAllRanges();
            }
        }

        function getData(str) {
            var requestURL = "http://adopraesenz.ipwin.ch/data/students.php?q=" +str;
            var request = new XMLHttpRequest();
            request.onreadystatechange = function() {           
            if(this.readyState === 4 && this.status === 200){
                    loadJson(request);
                }
            };
            request.open("GET", requestURL, true);
            request.send();
        }
        function getDataHistory(str) {
            var requestURL = "http://adopraesenz.ipwin.ch/data/history.php?q=" +str;
            var request = new XMLHttpRequest();
            request.onreadystatechange = function() {           
            if(this.readyState === 4 && this.status === 200){
                    if(request.responseText != ""){
                        loadDate(request);      
                    }
                }
            };
            request.open("GET", requestURL, true);
            request.send();
        }
        function getDataUser(str){
            var requestURL = "http://adopraesenz.ipwin.ch/data/login.php?q=" +str;
            var request = new XMLHttpRequest();
            request.onreadystatechange = function() {           
            if(this.readyState === 4 && this.status === 200){
                    if(request.responseText != ""){
                        loadDate(request);      
                    }                           }
            };
            request.open("GET", requestURL, true);
            request.send();
        }


        function getSelected(cell) {
            var value = document.getElementById("select").value;
            switch(value){
                case "Extern": return 0; break;
                case "Schule": return 1; break;
                case "Termin": return 2; break;
                default: return 3;
            }
        }


        function loadDate(request){
                var newDate = new Date();
                newDate.setDate(newDate.getDate() -1);
                newDate.setHours(0,0,0,0);
                var days = request.responseText.split(".");
                var oldDate = new Date(days[1]+"."+days[0]+"."+days[2]);

                if(newDate > oldDate){
                    var date = new Date();
                    date.setDate(date.getDate() - 1);
                    var dd = date.getDate();
                    var mm = date.getMonth() + 1;
                    var yyyy = date.getFullYear();
                    if(dd < 10) {
                        dd = '0' +dd;
                    }
                    if(mm < 10) {
                        mm = '0' +mm;
                    }
                    var yesterday = dd+"."+mm+"."+yyyy;
                    getDataHistory('add' + yesterday);
                    reset();
                }

                newDate = new Date().toLocaleDateString('de-CH', {
                    weekday: 'long',
                    day: '2-digit',
                    month: '2-digit',
                    year: 'numeric'
                });


                document.getElementById('date').innerHTML = newDate;

        }
        getDataHistory('new');

        function loadJson(request){
            createTable(request.responseText);
        }


        function createHeader(array){

            var header = document.createElement("thead");
            var hRow = document.createElement('tr');
            hRow.classList.add('header');
            for(var i = 0; i < array.length; i++){
                var div = document.createElement('div');
                var th = document.createElement('th');
                div.innerHTML = array[i];
                th.appendChild(div);
                hRow.appendChild(th);
            }
            header.appendChild(hRow);
            return header;
        }


        function createTable(json){ 

            var obj = JSON.parse(json);
            var oldBody = document.getElementsByTagName('tbody')[0];
            console.log(oldBody);
            var oldHeader = document.getElementsByTagName('thead')[0];
            var body = document.createElement('tbody');
            var header = createHeader(["Name","09:00 – 09:45","10:15 – 11:00","11:00 – 11:45"," ","14:00 – 14:45","15:00 - 15:45","16:00 – 16:45"]);
            for (var j = 0; j < obj.length; j++) {
                    var row = addRow(obj[j],body);
                    row.classList.add('row');   
            }
            console.log(body);
            replaceTable(body, oldBody, header ,oldHeader);

            if(obj.length > 25){
                var view = document.getElementById('home');
                view.setAttribute("style", "overflow-y:scroll");
            }
        }
        function toInput(cell){
            var input = document.createElement('input');
            setTimeout(function() { input.focus(); }, 200);
            cell.appendChild(input);

            window.addEventListener('keypress', function(e){
                if(e.keyCode == '13'){
                    var text = input.value;
                    if(input.parentNode != null){
                        input.parentNode.removeChild(input);
                    }
                    cell.innerHTML = text;
                    getData("get"+getString(cell.parentNode.cells[0].childNodes[0].innerHTML));
                }
            }, false);

        }

        function replaceTable(body, oldBody, header, oldHeader){
            if(typeof oldHeader == 'undefined'){
                    table.appendChild(header);
            }else if(oldHeader.parentNode == table){
                table.replaceChild(header, oldHeader);
            }else{
                table.appendChild(header);
            }


            if(typeof oldBody == 'undefined'){
                    table.appendChild(body);
            }else if(oldBody.parentNode == table){
                    table.removeChild(oldBody);
                    table.appendChild(body);
                    //table.replaceChild(body, oldBody);
            }else{
                table.appendChild(body);
            }

        }

        function addRow(val,body) {

            var rest = val.split(";");
            var tr = document.createElement('tr');
            for( var i = 0; i < 8; i++){

                if(i==0){
                    var name = rest[0];
                    addCell(tr, null,name);
                }else{
                    var value = rest[i];
                    addCell(tr, value, name);
                }

            }
        body.appendChild(tr);               
        return tr;
        }
        function addCell(tr, val, name) {


            var name;
            var cell = document.createElement('td');
            var value = "get";
            if(val == null){
                var input = document.createElement('label');
                cell.classList.add("name")
                input.innerHTML = name;
                input.readOnly = true;
                cell.appendChild(input);
            }else{ 
                cell = document.createElement('td');
                cell.classList.add('cell');
                var content = val.split(":");
                switch(content[0]){
                    case '0': cell.classList.add('ext'); break;
                    case '1': cell.classList.add('les'); break;
                    case '2': cell.classList.add('term'); break;
                    case '3': cell.classList.add('on'); break;
                    case '4': cell.classList.add('spacer'); break;
                }

                if(val.length > 1){
                    cell.innerHTML = content[1];
                }


            }
            tr.appendChild(cell);
        }


        window.onclick = function(event) {
          if (!event.target.matches('.dropA')) {
            var dropdowns = document.getElementsByClassName("dropdown-content");
            var i;
            for (i = 0; i < dropdowns.length; i++) {
              var openDropdown = dropdowns[i];
              if (openDropdown.classList.contains('show')) {
                openDropdown.classList.remove('show');
              }
            }
          }
        }

        function getString(name){
        var x = "";
        var names = document.getElementsByClassName('name');
        var values = document.getElementsByClassName('cell');
            for(var i = 0;i<names.length;i++){
                if(names[i].childNodes[0].innerHTML == name){

                    x+= names[i].childNodes[0].innerHTML + ";";
                    for(var j = (7 * i); j < (7 * i) + 7 ; j++){
                            switch(""+values[j].classList){
                                case 'cell':        x += "0"; break;
                                case 'cell ext':    x += "0"; break;
                                case 'cell les':    x += "1"; break;
                                case 'cell term':   x += "2"; break;
                                case 'cell on':     x += "3"; break;
                                case 'cell spacer': x += "4"; break;
                        }
                        if(values[j].innerHTML != "" && values[j].innerHTML != null){
                            x+= ":" + values[j].innerHTML
                        }
                        x += ";";
                    }

                }
            }   
        return x;
    }




    function getResetString(){
        var names = document.getElementsByClassName('name');
        var x = "";
        for(var i = 0; i < names.length; i++){
            x += names[i].value +";";
            for (var j = 0; j < 7 ; j++){
                if(j == 3){
                    x += "4";
                }else{
                    x += "0";
                }
            x += ";";
            }
            if(i < names.length-1){
                x+="|";
            }
        }
        return x;
    }

最佳答案

这似乎是个小问题。在您的 css 上,您必须在 calc 语句中添加一个空格:

height:calc(100% - 50px);

我能够让它在这里工作

https://codesandbox.io/s/vibrant-http-ty85k

关于html - 高度为 100% 的表格不适合父级大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59214904/

相关文章:

html - 无法对齐单选按钮列表

html - 如何仅使用 css 更改 html 元素的位置?

javascript - 如何增加 Highcharts 中某个点的可点击区域,以便每当该点为 'active' 时都可以记录点击?

html - 将表格放在可滚动 DIV 中时表格单元格换行?

javascript - 我的 Javascript for 循环有什么问题

html - 将图像垂直对齐到包含列表的 div 的底部

javascript - Bootstrap 2 - 设置对话框/模态位置

html - 如何让我的菜单不出现在 .navigation-bar>ul>li :hover? 上

html - Web 响应式 - 转换表格标题

javascript - 在日期选择器中选择多个日期