简体   繁体   English

如何创建具有固定左右列和水平滚动的表格?

[英]How do I create a table with fixed left and right columns and horizontal scrolling?

I'm trying to create a scheduling system and am having trouble with the grid.我正在尝试创建一个调度系统,但在使用网格时遇到了问题。 I want to have a table whose left and right th cells are fixed in place, but be able to scroll horizontally through the body td cells.我想要一个table其左右th单元格固定到位,但能够在 body td单元格中水平滚动。 The number of body td cells per tr is dynamic and can shrink and grow as the number of appointments in a time slot increases or decreases.每个tr的 body td单元数是动态的,并且可以随着时间段中约会数量的增加或减少而缩小和增长。 I've looked around and found similar questions here, but they only ask for the left column to be fixed, where as I need both.我环顾四周并在这里找到了类似的问题,但他们只要求修复左列,因为我需要两者。 I tried adapting what I saw in the answers, but it's not working.我尝试调整我在答案中看到的内容,但它不起作用。

My current prototype, here: https://jsfiddle.net/by2r6pkh/1/ ;我目前的原型,在这里: https : //jsfiddle.net/by2r6pkh/1/ is failing badly so I figured I'd ask for help.失败得很厉害,所以我想我会寻求帮助。 For what it's worth I'm only targeting Chrome for the browser.对于它的价值,我只针对浏览器使用 Chrome。

I don't think this is possible in CSS alone, but it doesn't take much JavaScript.我认为仅在 CSS 中这是不可能的,但它不需要太多的 JavaScript。

Change your TH elements from position: absolute to position: relative .将您的 TH 元素从position: absolute更改为position: relative That will resolve the problem with the wrapper's height, so you can remove overflow-y: visible .这将解决包装器高度的问题,因此您可以删除overflow-y: visible It also resolves the problem with the TH elements' height.它还解决了 TH 元素高度的问题。

You can also remove:您还可以删除:

  • position: relative; from the wrapper.从包装纸。
  • margin-left: 55px from the table. margin-left: 55px距离表格margin-left: 55px
  • left: 0; from th:first-child.来自:第一个孩子。
  • right: 0; from th:last-child.来自 th:last-child。

Vanilla JavaScript solution:原生 JavaScript 解决方案:

var wrapper = document.querySelector('.wrapper');

function scrollTable() {
  var thl = document.querySelectorAll('.wrapper th:nth-child(1)'),
      thr = document.querySelectorAll('.wrapper th:nth-last-child(1)');

  for(var i = 0; i < thl.length; i++) {
    thl[i].style.left = wrapper.scrollLeft + 'px';
    thr[i].style.right = wrapper.scrollWidth - wrapper.offsetWidth - wrapper.scrollLeft + 'px';
  }
} //scrollTable

wrapper.addEventListener('scroll', scrollTable);

scrollTable();

 var wrapper = document.querySelector('.wrapper'); function scrollTable() { var thl = document.querySelectorAll('.wrapper th:nth-child(1)'), thr = document.querySelectorAll('.wrapper th:nth-last-child(1)'); for(var i = 0; i < thl.length; i++) { thl[i].style.left = wrapper.scrollLeft + 'px'; thr[i].style.right = wrapper.scrollWidth - wrapper.offsetWidth - wrapper.scrollLeft + 'px'; } } //scrollTable wrapper.addEventListener('scroll', scrollTable); scrollTable();
 * { margin: 0; padding: 0; } body { margin: 32px; } .wrapper { overflow-x: scroll; /* scrolls the entire table, not just the tds */ width: 400px; } table { font: 14px arial; border-collapse: collapse; table-layout: fixed; /* fixes column widths, but overflows */ width: calc(100% - 110px); /* 2x left margin */ } td, th { border: 1px solid #CCC; padding: 2px; } td { background: #FFF; width: 150px; } th { position: relative; background: #EEE; width: 50px; }
 <div class="wrapper"> <table> <tr> <th>12:00 <br />pm</th> <td>{CONTENT}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <th>12:00 <br />pm</th> </tr> <tr> <th>12:30 <br />pm</th> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <th>12:30 <br />pm</th> </tr> </table> </div>


jQuery solution: jQuery 解决方案:

 $('.wrapper') .scroll(function() { $('.wrapper th:nth-child(1)').css('left', this.scrollLeft); $('.wrapper th:nth-last-child(1)').css( 'right', this.scrollWidth - this.offsetWidth - this.scrollLeft ); }) .scroll(); 

 $('.wrapper') .scroll(function() { $('.wrapper th:nth-child(1)').css('left', this.scrollLeft); $('.wrapper th:nth-last-child(1)').css('right', this.scrollWidth - this.offsetWidth - this.scrollLeft); }) .scroll();
 * { margin: 0; padding: 0; } body { margin: 32px; } .wrapper { overflow-x: scroll; /* scrolls the entire table, not just the tds */ width: 400px; } table { font: 14px arial; border-collapse: collapse; table-layout: fixed; /* fixes column widths, but overflows */ width: calc(100% - 110px); /* 2x left margin */ } td, th { border: 1px solid #CCC; padding: 2px; } td { background: #FFF; width: 150px; } th { position: relative; background: #EEE; width: 50px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <table> <tr> <th>12:00 <br />pm</th> <td>{CONTENT}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <th>12:00 <br />pm</th> </tr> <tr> <th>12:30 <br />pm</th> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <th>12:30 <br />pm</th> </tr> </table> </div>

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

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