简体   繁体   English

具有固定列的可滚动表,固定大小相等

[英]Scrollable table with fixed column fixed with equal size

I have a table with first column fixed so when the next column is scrolled first column item will always display, Now the problem is first column width have fixed value and the same is applying on the margin left, How can we make fluid width instead of fixed width so it resizes based on the column?我有一个第一列固定的表格,所以当滚动下一列时,第一列项目将始终显示,现在问题是第一列宽度有固定值,同样适用于左边距,我们如何制作流体宽度而不是固定宽度,以便根据列调整大小?

 .table-main { border: none; border-right: solid 1px rgb(75, 90, 102); border-collapse: separate; border-spacing: 0; font: normal 13px Arial, sans-serif; } .table-main thead th { background-color: rgb(203, 220, 233); border: none; color: #336B6B; padding: 10px; text-align: left; text-shadow: 1px 1px 1px #fff; white-space: nowrap; } .table-main tbody td { border-bottom: solid 1px rgb(75, 90, 102); color: #333; padding: 10px; text-shadow: 1px 1px 1px #fff; white-space: nowrap; } .table { position: relative; } .table-scroll { margin-left: 131px; /*HOW TO HAVE THIS in Eqaul SIZE??*/ overflow-x: scroll; overflow-y: visible; padding-bottom: 5px; width: 500px; } .table-main .fix-col { border-left: solid 1px rgb(75, 90, 102); border-right: solid 1px rgb(75, 90, 102); left: 0; position: absolute; top: auto; width: 110px; /*HOW TO HAVE THIS in Eqaul SIZE??*/ }
 <div class="table"> <div class="table-scroll"> <table class="table-main"> <thead> <tr> <th class="fix-col">Name</th> <th>Designation</th> <th>Experience</th> <th>Technology</th> <th>Company</th> <th>Location</th> <th>Contact No.</th> <th>Address</th> </tr> </thead> <tbody> <tr> <td class="fix-col">Bob</td> <td>Front End Developer</td> <td>5 yrs</td> <td>HTML,CSS</td> <td>Google</td> <td>California</td> <td>9876543210</td> <td>Google Office</td> </tr> <tr> <td class="fix-col">Bob</td> <td>Front End Developer</td> <td>5 yrs</td> <td>HTML,CSS</td> <td>Google</td> <td>California</td> <td>9876543210</td> <td>Google Office</td> </tr> <tr> <td class="fix-col">Bob</td> <td>Front End Developer</td> <td>5 yrs</td> <td>HTML,CSS</td> <td>Google</td> <td>California</td> <td>9876543210</td> <td>Google Office</td> </tr> <tr> <td class="fix-col">Bob</td> <td>Front End Developer</td> <td>5 yrs</td> <td>HTML,CSS</td> <td>Google</td> <td>California</td> <td>9876543210</td> <td>Google Office</td> </tr> <tr> <td class="fix-col">Bob</td> <td>Front End Developer</td> <td>5 yrs</td> <td>HTML,CSS</td> <td>Google</td> <td>California</td> <td>9876543210</td> <td>Google Office</td> </tr> </tbody> </table> </div> </div>

What about using javascript to calculate and set the width of the fixed column( .fix-col ) and the margin-left of the container div( .table-scroll )?使用javascript计算和设置固定列的宽度( .fix-col )和容器div( .table-scroll )的margin-left怎么样?

 var columns = document.querySelectorAll('.fix-col'); var maxWidth = 0; /* Loop through columns to get the widest one */ for (var i = 0; i < columns.length; i++) { /* Get only the width, without any paddings */ var w = parseFloat(window.getComputedStyle(columns[i]).getPropertyValue('width')); if (w > maxWidth) { maxWidth = w; } } /* Second loop to set the width */ for (var i = 0; i < columns.length; i++) { columns[i].style.width = maxWidth + 'px'; } /* And finally update the margin of the wrapping div */ var paddingPlusBorder = 21; document.querySelector('.table-scroll').style.marginLeft = maxWidth + paddingPlusBorder + 'px';
 .table-main { border: none; border-right: solid 1px rgb(75, 90, 102); border-collapse: separate; border-spacing: 0; font: normal 13px Arial, sans-serif; } .table-main thead th { background-color: rgb(203, 220, 233); border: none; color: #336B6B; padding: 10px; text-align: left; text-shadow: 1px 1px 1px #fff; white-space: nowrap; } .table-main tbody td { border-bottom: solid 1px rgb(75, 90, 102); color: #333; padding: 10px; text-shadow: 1px 1px 1px #fff; white-space: nowrap; } .table { position: relative; } .table-scroll { overflow-x: scroll; overflow-y: visible; padding-bottom: 5px; width: 500px; } .table-main .fix-col { border-left: solid 1px rgb(75, 90, 102); border-right: solid 1px rgb(75, 90, 102); left: 0; position: absolute; top: auto; }
 <div class="table"> <div class="table-scroll"> <table class="table-main" id="my-table"> <thead> <tr> <th class="fix-col">Name</th> <th>Designation</th> <th>Experience</th> <th>Technology</th> <th>Company</th> <th>Location</th> <th>Contact No.</th> <th>Address</th> </tr> </thead> <tbody> <tr> <td class="fix-col">Some very long name</td> <td>Front End Developer</td> <td>5 yrs</td> <td>HTML,CSS</td> <td>Google</td> <td>California</td> <td>9876543210</td> <td>Google Office</td> </tr> <tr> <td class="fix-col">LooooooooooongNameeee</td> <td>Front End Developer</td> <td>5 yrs</td> <td>HTML,CSS</td> <td>Google</td> <td>California</td> <td>9876543210</td> <td>Google Office</td> </tr> <tr> <td class="fix-col">Bob</td> <td>Front End Developer</td> <td>5 yrs</td> <td>HTML,CSS</td> <td>Google</td> <td>California</td> <td>9876543210</td> <td>Google Office</td> </tr> </tbody> </table> </div> </div>

And if you want to support older IE versions you can use a custom function to get the column widths instead of using window.getComputedStyle(columns[i]).getPropertyValue('width') .如果您想支持较旧的 IE 版本,您可以使用自定义函数来获取列宽,而不是使用window.getComputedStyle(columns[i]).getPropertyValue('width')

function getWidth(element) {
  if (getComputedStyle in window) {
    return window.getComputedStyle(element).getPropertyValue('width');
  }

  if (currentStyle in element) {
    return element.currentStyle.width;
  } 

  var defaultWidthIfNoSupport = '100px';
  return defaultWidthIfNoSupport;
}

You could use position: sticky Sticky Element in order to anchor to left the fixed column:您可以使用position: sticky Sticky Element以锚定到固定列的左侧:

.fix-col {
    position: sticky;
    position: -webkit-sticky;    
    background-color: white; // <-- By default it's transparent
}

<table>
    <tr>
        <th class="fix-col">Name</th>

see the code applied to your snippet:查看应用于您的代码段的代码:

 .table-main { border: none; border-right: solid 1px rgb(75, 90, 102); border-collapse: separate; border-spacing: 0; font: normal 13px Arial, sans-serif; } .table-main thead th { background-color: rgb(203, 220, 233); border: none; color: #336B6B; padding: 10px; text-align: left; text-shadow: 1px 1px 1px #fff; white-space: nowrap; } .table-main tbody td { border-bottom: solid 1px rgb(75, 90, 102); color: #333; padding: 10px; text-shadow: 1px 1px 1px #fff; white-space: nowrap; } .table-scroll { overflow-x: scroll; width: 500px; } .fix-col { position: sticky; position: -webkit-sticky; background-color: white; left: 0; margin-left: 1em; }
 <div class="table"> <div class="table-scroll"> <table class="table-main"> <thead> <tr> <th class="fix-col">Name</th> <th>Designation</th> <th>Experience</th> <th>Technology</th> <th>Company</th> <th>Location</th> <th>Contact No.</th> <th>Address</th> </tr> </thead> <tbody> <tr> <td class="fix-col">Bob</td> <td>Front End Developer</td> <td>5 yrs</td> <td>HTML,CSS</td> <td>Google</td> <td>California</td> <td>9876543210</td> <td>Google Office</td> </tr> <tr> <td class="fix-col">John</td> <td>Front End Developer</td> <td>5 yrs</td> <td>HTML,CSS</td> <td>Google</td> <td>California</td> <td>9876543210</td> <td>Google Office</td> </tr> <tr> <td class="fix-col">Alessandro</td> <td>Front End Developer</td> <td>5 yrs</td> <td>HTML,CSS</td> <td>Google</td> <td>California</td> <td>9876543210</td> <td>Google Office</td> </tr> <tr> <td class="fix-col">Joseph</td> <td>Front End Developer</td> <td>5 yrs</td> <td>HTML,CSS</td> <td>Google</td> <td>California</td> <td>9876543210</td> <td>Google Office</td> </tr> <tr> <td class="fix-col">Mark</td> <td>Front End Developer</td> <td>5 yrs</td> <td>HTML,CSS</td> <td>Google</td> <td>California</td> <td>9876543210</td> <td>Google Office</td> </tr> </tbody> </table> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM