2017-05-24 47 views
0

我玩过很多选项,但是我无法保持表格高度不会随着动态添加线条而增加。无法使表格高度与对齐方式在div中

这是一个小部分,是一个更复杂页面的一部分。基本上我有更大的容器div内的几个div标签。

随着更多行被添加,表格将按钮推到div的边界之外。运行代码片段来观察问题。

function onBodyLoader(obj) { 
 
    g.c.assignEventListners(); 
 
} 
 
var g = {}; 
 

 
g.formClass = function() { 
 
    /* 
 
    ---------------------------------- 
 
    Properties for formClass 
 
    ---------------------------------- 
 
    */ 
 
    this.tr; 
 
    this.td; 
 
    this.elist = []; 
 

 
    /* 
 
    ---------------------------------- 
 
    Methods for formClass 
 
    ---------------------------------- 
 
    */ 
 

 
    this.assignEventListners = function() { 
 
    this.tbody = document.getElementById('emailDist'); 
 

 
    g.sbAddELine = document.getElementById('sbAddELine'); 
 
    g.sbAddELine.addEventListener("click", function(evt) { 
 
     g.c.addBlank(); 
 
    }, false); 
 

 
    /*event listener for all links on the email list body*/ 
 
    g.dataUpdate = document.querySelector("#emailDist"); 
 
    g.dataUpdate.addEventListener("click", g.c.tableBodyRouter, false); 
 

 
    }; 
 

 
    this.tableBodyRouter = function(e) { 
 
    /* 
 
    called from clicks on keyTable or task links 
 
    */ 
 
    if (e.target !== e.currentTarget) 
 
     if (e.target.id.indexOf('eRemove') > -1) 
 
     g.c.removeEmail(e); 
 
    e.stopPropagation(); 
 
    }; 
 

 
    this.redrawElist = function() { 
 

 
    /*delete current table*/ 
 
    while (this.tbody.rows.length > 1) 
 
     this.tbody.deleteRow(1); 
 

 
    /*redraw table*/ 
 
    for (var i = 0; i < this.elist.length; i++) { 
 
     this.rowLayout(); 
 
    } 
 
    }; 
 

 
    this.addBlank = function() { 
 
    /*add blank to this.elist array*/ 
 
    this.elist.push({ 
 
     eEmail: '', 
 
     eFirst: '', 
 
     eLast: '', 
 
    }); 
 

 
    this.rowLayout(); 
 

 
    } 
 
    this.removeEmail = function(e) { 
 
    var x = e.target.id.substr(7); 
 

 
    this.elist.splice(x, 1); 
 
    this.redrawElist(); 
 

 
    }; 
 

 
    this.rowLayout = function() { 
 
    var rowCnt = this.tbody.rows.length - 1; 
 
    this.tr = this.tbody.insertRow(this.tbody.rows.length); 
 

 
    this.td = this.tr.insertCell(this.tr.cells.length); 
 
    this.td.innerHTML = '<input type="text" id="eFirst' + rowCnt + '" maxlength="20" size="20" value=""/>'; 
 

 
    this.td = this.tr.insertCell(this.tr.cells.length); 
 
    this.td.innerHTML = '<input type="text" id="eLast' + rowCnt + '" maxlength="20" size="20" value="" />'; 
 

 
    this.td = this.tr.insertCell(this.tr.cells.length); 
 
    this.td.innerHTML = '<input type="text" id="eEmail' + rowCnt + '" maxlength="50" size="50" value="" />'; 
 

 
    this.td = this.tr.insertCell(this.tr.cells.length); 
 
    this.td.innerHTML = '<input type="button" id="eRemove' + rowCnt + '" value="Remove" ">'; 
 
    document.getElementById("eFirst" + rowCnt).focus(); 
 
    document.getElementById("eFirst" + rowCnt).select(); 
 
    } 
 

 
} 
 
g.c = new g.formClass;
table { 
 
    height: 60%; 
 
    max-height: 60%; 
 
    width: 100%; 
 
    display: inline-table; 
 
    border-style: none; 
 
} 
 

 
tbody { 
 
    font-size: 10pt; 
 
    display: block; 
 
    height: 90%; 
 
    overflow-y: scroll; 
 
} 
 

 
#container { 
 
    position: absolute; 
 
    width: 98%; 
 
    top: 40px; 
 
    height: 90%; 
 
} 
 

 
#dataEntryDiv { 
 
    border: medium groove; 
 
    position: absolute; 
 
    top: 0.5em; 
 
    height: 95%; 
 
    padding-left: 1em; 
 
    padding-right: 1em; 
 
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
 

 
<html lang="en"> 
 

 
<head> 
 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
 
    <title>Email List</title> 
 
</head> 
 

 
<body id="intactRolesBody" onLoad="onBodyLoader(this);"> 
 
    <form id='intactRolesForm' method="post" action="" onSubmit="return false;"> 
 
    <div id="container"> 
 
     <div id="dataEntryDiv"> 
 
     <input type="button" id='sbAddELine' value="Add non-company contact"><br> 
 
     <p>Email Distribution List</p> 
 
     <table> 
 
      <tbody id='emailDist'> 
 
      <tr> 
 
       <th>First Name</th> 
 
       <th>Last Name</th> 
 
       <th>email</th> 
 
       <th>remove from list</th> 
 
      </tr> 
 
      </tbody> 
 
     </table> 
 
     <input type="button" id='SaveEmailList' value="Save email List"> 
 
     </div> 
 
    </div> 
 
    </form> 
 
</body> 
 

 
</html>

+0

如何在表格中添加'''overflow:hidden;'''style? –

+0

将无法​​保持表的大小不变 – Claus

回答

1

这是一个表的基本行为。根据其内容缩小和扩大。

你可以做什么来管理高度是重置显示。 它可以是除table/inline-table/table-cell/table-row/..之外的任何东西。也不内联。

您使用内联表,inline-block的可能是罚款:

function onBodyLoader(obj) { 
 
    g.c.assignEventListners(); 
 
} 
 
var g = {}; 
 

 
g.formClass = function() { 
 
    /* 
 
    ---------------------------------- 
 
    Properties for formClass 
 
    ---------------------------------- 
 
    */ 
 
    this.tr; 
 
    this.td; 
 
    this.elist = []; 
 

 
    /* 
 
    ---------------------------------- 
 
    Methods for formClass 
 
    ---------------------------------- 
 
    */ 
 

 
    this.assignEventListners = function() { 
 
    this.tbody = document.getElementById('emailDist'); 
 

 
    g.sbAddELine = document.getElementById('sbAddELine'); 
 
    g.sbAddELine.addEventListener("click", function(evt) { 
 
     g.c.addBlank(); 
 
    }, false); 
 

 
    /*event listener for all links on the email list body*/ 
 
    g.dataUpdate = document.querySelector("#emailDist"); 
 
    g.dataUpdate.addEventListener("click", g.c.tableBodyRouter, false); 
 

 
    }; 
 

 
    this.tableBodyRouter = function(e) { 
 
    /* 
 
    called from clicks on keyTable or task links 
 
    */ 
 
    if (e.target !== e.currentTarget) 
 
     if (e.target.id.indexOf('eRemove') > -1) 
 
     g.c.removeEmail(e); 
 
    e.stopPropagation(); 
 
    }; 
 

 
    this.redrawElist = function() { 
 

 
    /*delete current table*/ 
 
    while (this.tbody.rows.length > 1) 
 
     this.tbody.deleteRow(1); 
 

 
    /*redraw table*/ 
 
    for (var i = 0; i < this.elist.length; i++) { 
 
     this.rowLayout(); 
 
    } 
 
    }; 
 

 
    this.addBlank = function() { 
 
    /*add blank to this.elist array*/ 
 
    this.elist.push({ 
 
     eEmail: '', 
 
     eFirst: '', 
 
     eLast: '', 
 
    }); 
 

 
    this.rowLayout(); 
 

 
    } 
 
    this.removeEmail = function(e) { 
 
    var x = e.target.id.substr(7); 
 

 
    this.elist.splice(x, 1); 
 
    this.redrawElist(); 
 

 
    }; 
 

 
    this.rowLayout = function() { 
 
    var rowCnt = this.tbody.rows.length - 1; 
 
    this.tr = this.tbody.insertRow(this.tbody.rows.length); 
 

 
    this.td = this.tr.insertCell(this.tr.cells.length); 
 
    this.td.innerHTML = '<input type="text" id="eFirst' + rowCnt + '" maxlength="20" size="20" value=""/>'; 
 

 
    this.td = this.tr.insertCell(this.tr.cells.length); 
 
    this.td.innerHTML = '<input type="text" id="eLast' + rowCnt + '" maxlength="20" size="20" value="" />'; 
 

 
    this.td = this.tr.insertCell(this.tr.cells.length); 
 
    this.td.innerHTML = '<input type="text" id="eEmail' + rowCnt + '" maxlength="50" size="50" value="" />'; 
 

 
    this.td = this.tr.insertCell(this.tr.cells.length); 
 
    this.td.innerHTML = '<input type="button" id="eRemove' + rowCnt + '" value="Remove" ">'; 
 
    document.getElementById("eFirst" + rowCnt).focus(); 
 
    document.getElementById("eFirst" + rowCnt).select(); 
 
    } 
 

 
} 
 
g.c = new g.formClass;
table { 
 
    height: 60%; 
 
    max-height: 60%; 
 
    width: 100%; 
 
    display: inline-block;/*... or block : do not use table display if you need to constrain height */ 
 
    border-style: none; 
 
    } 
 
    
 
    tbody {/* this CSS could have been set to table directly :) */ 
 
    font-size: 10pt; 
 
    display: block; 
 
    height: 90%; 
 
    overflow-y: scroll; 
 
    } 
 
    
 
    #container { 
 
    position: absolute; 
 
    width: 98%; 
 
    top: 40px; 
 
    height: 90%; 
 
    } 
 
    
 
    #dataEntryDiv { 
 
    border: medium groove; 
 
    position: absolute; 
 
    top: 0.5em; 
 
    /*left: 37em; removed for demo */ 
 
    height: 95%; 
 
    padding-left: 1em; 
 
    padding-right: 1em; 
 
    }
<body id="intactRolesBody" onLoad="onBodyLoader(this);"> 
 
    <form id='intactRolesForm' method="post" action="" onSubmit="return false;"> 
 
    <div id="container"> 
 
     <div id="dataEntryDiv"> 
 
     <input type="button" id='sbAddELine' value="Add non-company contact"><br> 
 
     <p>Email Distribution List</p> 
 
     <table> 
 
      <tbody id='emailDist'> 
 
      <tr> 
 
       <th>First Name</th> 
 
       <th>Last Name</th> 
 
       <th>email</th> 
 
       <th>remove from list</th> 
 
      </tr> 
 
      </tbody> 
 
     </table> 
 
     <input type="button" id='SaveEmailList' value="Save email List"> 
 
     </div> 
 
    </div> 
 
    </form> 
 
</body>


注意:您没上tbody使用display:block,你可以有直接将此到table元件和复位tbodydisplay:table :)(默认为table-row-group

+0

感谢您的详细解释。我以全屏模式运行你的代码片段,并看到它完成了我需要做的一切。 – Claus

0

添加

#dataEntryDiv { 
    overflow: auto; 
} 
+0

感谢您的建议,我担心我需要更多这一点。请注意我使用overflow-y来控制垂直。 – Claus

0

为了得到一个简化版本的情况,我会建议写这样的东西 - 而不是从你的实际项目中放入代码。通过这种方式,您可以避免尝试“修复”某些东西 - 并且可能会看到更好的方式来构建布局 - 或者至少使用例更具体。

https://stackoverflow.com/help/how-to-ask


标记

<section class="table-wrapper"> 
    <header> 
    I'm a table wrapper thing 
    </header> 

    <main> 
    <table> 
     <!-- populate this --> 
    </table> 
    </main> 

    <footer> 
    <button>button (add row)</button> 
    </footer> 
</section> 


风格

.table-wrapper { 
    height: 300px; /* arbitrary */ 
    border: 2px solid red; 
} 

.table-wrapper main { 
    height: 260px; /* likely you'd use flexbox or percentages or JS */ 
    border: 2px solid blue; 
    overflow: auto; 
} 


JS

var $table = $('.table-wrapper').find('table'); 
var $moreButton = $('.table-wrapper').find('button'); 

var counter = 0; 

function addRow() { 
    counter = counter + 1; 
    $table.prepend('<tr><td>row and data ' + counter + '</td></tr>'); 
} 

addRow(); 
// populate some things to start 

$moreButton.on('click', function() { 
    addRow(); 
}); 

https://jsfiddle.net/sheriffderek/b6z4ep46/

+0

谢谢您的建议。我明白你在说什么简化我的问题,好点,这正是我想要做的。我不做jQuery,所以我不能实现你的解决方案。另外,因为我需要跟踪动态列表中的其他信息,所以我注意到,您的示例使用prepend方法将新行添加到表顶部,这使得我更难从列表中跟踪删除。但是,我确实注意到您的解决方案确实保持了桌子的高度,并且它是容器。再次感谢您的帮助。 – Claus

+0

这些概念不是jQuery特有的。你可以改变prepend来追加。我确信你可以看到所做的要点和布局选择 - 并将其应用到你的项目中。 : ) 祝你好运! – sheriffderek