2017-09-14 29 views
0

我試過了由Hashem Qolami發佈在HTML table with 100% width, with vertical scroll inside tbody後的解決方案,但我遇到了問題。100%寬度的HTML表格,在沒有td的tbody內部有垂直滾動wdth

Tried Scenario-Issue

讓我填寫你,如果你需要更多的細節。

我試過多篇文章。我發現在tbodytd只有固定的寬度。但根據tbody中的數據,我需要一個動態寬度。

它在網站中工作時有js,css的不同部分。 但是,當我將所有內容組合到單個html中時,這種方式並不工作。 我的HTML圖像:調整大小時HTML Image

// Change the selector if needed 
 
var $table = $('table.scroll'), 
 
    $bodyCells = $table.find('tbody tr:first').children(), 
 
    colWidth; 
 

 
// Adjust the width of thead cells when window resizes 
 
$(window).resize(function() { 
 
    // Get the tbody columns width array 
 
    colWidth = $bodyCells.map(function() { 
 
    return $(this).width(); 
 
    alert("hii"); 
 
    }).get(); 
 

 
    // Set the width of thead columns 
 
    $table.find('thead tr').children().each(function(i, v) { 
 
    $(v).width(colWidth[i]); 
 
    }); 
 
}).resize(); // Trigger resize handler
table.scroll { 
 
    /* width: 100%; */ 
 
    /* Optional */ 
 
    /* border-collapse: collapse; */ 
 
    border-spacing: 0; 
 
    border: 2px solid black; 
 
} 
 

 
table.scroll tbody, 
 
table.scroll thead { 
 
    display: block; 
 
} 
 

 
thead tr th { 
 
    height: 30px; 
 
    line-height: 30px; 
 
    /* text-align: left; */ 
 
} 
 

 
table.scroll tbody { 
 
    height: 100px; 
 
    overflow-y: auto; 
 
    overflow-x: hidden; 
 
} 
 

 
tbody { 
 
    border-top: 2px solid black; 
 
} 
 

 
tbody td, 
 
thead th { 
 
    /* width: 20%; */ 
 
    /* Optional */ 
 
    border-right: 1px solid black; 
 
    /* white-space: nowrap; */ 
 
} 
 

 
tbody td:last-child, 
 
thead th:last-child { 
 
    border-right: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
 

 
<table class="scroll"> 
 
    <thead> 
 
    <tr> 
 
     <th>Head 1</th> 
 
     <th>Head 2</th> 
 
     <th>Head 3</th> 
 
     <th>Head 4</th> 
 
     <th>Head 5</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td>Content 1</td> 
 
     <td>Content 2</td> 
 
     <td>Content 3</td> 
 
     <td>Content 4</td> 
 
     <td>Content 5</td> 
 
    </tr> 
 
    <tr> 
 
     <td>Content 1</td> 
 
     <td>Lorem ipsum dolor sit amet.</td> 
 
     <td>Content 3</td> 
 
     <td>Content 4</td> 
 
     <td>Content 5</td> 
 
    </tr> 
 
    <tr> 
 
     <td>Content 1</td> 
 
     <td>Content 2</td> 
 
     <td>Content 3</td> 
 
     <td>Content 4</td> 
 
     <td>Content 5</td> 
 
    </tr> 
 
    <tr> 
 
     <td>Content 1</td> 
 
     <td>Content 2</td> 
 
     <td>Content 3</td> 
 
     <td>Content 4</td> 
 
     <td>Content 5</td> 
 
    </tr> 
 
    <tr> 
 
     <td>Content 1</td> 
 
     <td>Content 2</td> 
 
     <td>Content 3</td> 
 
     <td>Content 4</td> 
 
     <td>Content 5</td> 
 
    </tr> 
 
    <tr> 
 
     <td>Content 1</td> 
 
     <td>Content 2</td> 
 
     <td>Content 3</td> 
 
     <td>Content 4</td> 
 
     <td>Content 5</td> 
 
    </tr> 
 
    <tr> 
 
     <td>Content 1</td> 
 
     <td>Content 2</td> 
 
     <td>Content 3</td> 
 
     <td>Content 4</td> 
 
     <td>Content 5</td> 
 
    </tr> 
 
    </tbody> 
 
</table>

回答

4

取消註釋你的表格寬度

table.scroll { 
    width: 100%; 
    /* Optional */ 
    /* border-collapse: collapse; */ 
    border-spacing: 0; 
    border: 2px solid black; 
} 

請嘗試this

細胞相應的響應。

+0

感謝您的幫助。但我確定當我試圖運行整個HTML它沒有工作時,我什麼是錯的。當我嘗試使用這些其他代碼模板源時,它工作正常。你可以請建議..我已經添加了我的HTML截圖作爲圖像在編輯.. – Harry

相關問題