2017-03-16 39 views
1

我試圖創建一個4列的表和至少12行與overflow-y:scroll。該頁面完全響應。我明白,如果我需要添加一個滾動條到桌子上,我需要使它顯示顯示:thead以及tbody。現在我面臨的問題是我不能讓我的曲目有100%的寬度。我試圖調整我的代碼幾個小時,似乎無法達成任何解決方案。連接下面的jsfiddle鏈接如何做一個表內的滾動條,以及使行佔100%的寬度

https://jsfiddle.net/kuau4ace/ ` 我只是試圖對此使用HTML和CSS。

請幫忙! <table>

回答

0

通過使THEAD和TBODY到顯示:塊你改變表的屬性,這就是爲什麼TR不拿寬度:100%。

試試這個代碼

$(document).ready(function(){ 
 
    var body=$('.template_table').clone(); 
 
    body.addClass('body'); 
 
    $('.table-wrapper').append("<div class='table-body'></div>"); 
 
    $('.table-body').append(body); 
 
    var curr = []; 
 
    $("table.body th").each(function(i) { 
 
\t \t \t \t var tColumnWidth = $(this).width(); 
 
\t \t \t \t curr.push(tColumnWidth); 
 
    }); 
 
    $("table:first-child th").each(function(i){ 
 
    \t \t $(this).width(curr[i]); 
 
    }); 
 
    $("table.body thead").empty(); 
 
    $(".table-wrapper>table tbody").empty(); 
 
});
.template_table{ 
 
    border:1px solid #000; 
 
    width:100%; 
 
    padding: 0; 
 
} 
 
.template_table thead{ 
 
    background:#323333; 
 
    height: 30px; 
 
    width: 100%; 
 
} 
 
.template_table tbody{ 
 
    height: 210px; 
 
    width: 100%; 
 
    overflow-x: hidden; 
 
    overflow-y: auto; 
 
} 
 
.template_table th{ 
 
    border:1px solid #000; 
 
    border-left:0; 
 
    border-right:0 
 
} 
 
.template_table th select{ 
 
    background:none; 
 
    border:none; 
 
    border-right:1px solid #323232; 
 
    color:#fff; 
 
    padding:5px; 
 
    width:80%; 
 
} 
 
.template_table th{ 
 
    width:330px; 
 
} 
 
.template_table th:first-child{ 
 
    width:80px; 
 
    border:1px solid #000; 
 
} 
 
.template_table th:nth-child(2){ 
 
    width:220px; 
 
    border:1px solid #000; 
 
} 
 
.template_table td:first-child{ 
 
    width:80px; 
 
} 
 
.template_table td:nth-child(2){ 
 
    width:220px; 
 
} 
 
.template_table td{ 
 
    width:220px; 
 
    color: #c3bfbf; 
 
    border-bottom:1px solid #000; 
 
} 
 
.template_table tbody tr:hover td{ 
 
    background:#676767; 
 
    color:#fff; 
 
} 
 

 
.template_table th{ 
 
    border:1px solid #000; 
 
    border-left:0; 
 
    border-right:0; 
 
} 
 
.template_table th select{ 
 
    background:none; 
 
    border:none; 
 
    border-right:1px solid #323232; 
 
    color:#fff; 
 
    padding:5px; 
 
    width:80%; 
 
} 
 
.template_table th:first-child{ 
 
    width:80px; 
 
    border:1px solid #000; 
 
} 
 
.template_table th:nth-child(2){ 
 
    width:220px; 
 
    border:1px solid #000; 
 
} 
 
.template_table td:first-child{ 
 
    width:17%; 
 
} 
 
.template_table td:nth-child(2){ 
 
    width:37%; 
 
} 
 
.template_table td:nth-child(3) { 
 

 
} 
 
.template_table td{ 
 
    color:#807c7c; 
 
    border-bottom:1px solid #000; 
 
} 
 
.template_table tbody tr:hover td{ 
 
    background:#676767; 
 
    color:#fff; 
 
} 
 
tbody tr{ 
 
    cursor:pointer; 
 
    } 
 
.table-wrapper { 
 
    width: calc(100% - 20px); 
 
} 
 
.table-body { 
 
    height: 200px; 
 
    overflow-x: hidden; 
 
    overflow-y: auto; 
 
    width: calc(100% + 15px); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
 
<div class="table-wrapper"> 
 
<table class="col-xs-12 template_table"> 
 
           <thead> 
 
           <tr> 
 
            <th></th> 
 
            <th> 
 
             <select> 
 
              <option>Page Name </option> 
 
             </select> 
 
            </th> 
 
            <th> 
 
             <select> 
 
              <option>Template</option> 
 
             </select> 
 
            </th> 
 
           </tr> 
 
           </thead> 
 
           <tbody> 
 
           <tr> 
 
            <td></td> 
 
            <td>Video Source 1 </td> 
 
            <td>Video Clip</td> 
 
           </tr> 
 
           <tr> 
 
            <td></td> 
 
            <td>ManningL3 </td> 
 
            <td>Player L3</td> 
 

 
           </tr> 
 
           <tr> 
 
            <td></td> 
 
            <td>Boyriven </td> 
 
            <td>Talent ID</td> 
 
           </tr> 
 
           <tr> 
 
            <td></td> 
 
            <td>Halftime Stats </td> 
 
            <td>Full Screen Stats</td> 
 
           </tr> 
 
           <tr> 
 
            <td></td> 
 
            <td>Locator </td> 
 
            <td>Full Screen Locator</td> 
 
           </tr> 
 
           <tr> 
 
            <td></td> 
 
            <td>Locator </td> 
 
            <td>Full Screen Locator</td> 
 
           </tr> 
 
           <tr> 
 
            <td></td> 
 
            <td>Locator </td> 
 
            <td>Full Screen Locator</td> 
 
           </tr> 
 
           <tr> 
 
            <td></td> 
 
            <td>Time </td> 
 
            <td>Bug</td> 
 
           </tr> 
 
           <tr> 
 
            <td></td> 
 
            <td>Singular Info </td> 
 
            <td>Topic L3</td> 
 
           </tr> 
 
           <tr> 
 
            <td></td> 
 
            <td>Los Angeles</td> 
 
            <td>Locator</td> 
 
           </tr> 
 
           <tr> 
 
            <td></td> 
 
            <td>Lewandowski </td> 
 
            <td>Player L3</td> 
 
           </tr> 
 
           <tr> 
 
            <td></td> 
 
            <td>Lewandowski </td> 
 
            <td>Player L3</td> 
 
           </tr> 
 
           </tbody> 
 
          </table> 
 
          </div>

+0

您的代碼解決1分的問題,而是滾動應用於THEAD爲好。另外,對th的寬度進行硬編碼不會使表格響應。 –

+0

您好,請在上面找到我的更新。我已經寫了一些js使頭固定。希望這可以幫助.... – Amal

相關問題