2017-05-24 37 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