【问题标题】:Table with scrollable rows and fixed header具有可滚动行和固定标题的表格
【发布时间】:2014-05-22 02:17:06
【问题描述】:

我有基于this solutionthis code,但它似乎对我不起作用。

我希望能够:

  • 滚动行
  • 要修复的标头
  • 标题宽度与第一行 TD 相同。

我对 JavaScript 很陌生,所以我不知道我错过了什么。滚动部分正在工作,但不在 jsfiddle 中。如何使TH 的宽度与TD 的宽度相同?

编辑 1:

代码

<html>
<head>
    <style>
        .outerDIV {
            position: relative;
            padding-top: 30px;   //height of your thead
        }
        .innerDIV {
            overflow: auto;
            max-height: 200;       //the actual scrolling container
        }
        table thead {
            display: block;
            position: absolute;
            top: 0;
            left: 0;
        }
        table tbody {
            display: block;
        }
    </style>
    <script>
        function scrollingTableSetThWidth(tableId)
        {
            var table = document.getElementById(tableId);

            ths = table.getElementsByTagName('th');
            tds = table.getElementsByTagName('td');

            if(ths.length > 0) {
                for(i=0; i < ths.length; i++) {
                    ths[i].style.width = getCurrentComputedStyle(tds[i], 'width');
                }
            }
        }

        function getCurrentComputedStyle(element, attribute)
        {
            var attributeValue;
            if (window.getComputedStyle) 
            { // class A browsers
                var styledeclaration = document.defaultView.getComputedStyle(element, null);
                attributeValue = styledeclaration.getPropertyValue(attribute);
            } else if (element.currentStyle) { // IE
                attributeValue = element.currentStyle[vclToCamelCases(attribute)];
            }
            return attributeValue;
        }
    </script>

</head>
<body>
    <div class="outerDIV">
        <div class="innerDIV">
            <table border="1">
                <thead>
                    <tr>
                        <div><th>Column1</th><th>Column2</th><th>Column3</th></div>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Line1Cell1</td><td>Line1Cell2</td><td>Line1Cell3</td>
                    </tr>
                    <tr>
                        <td>Line2Cell1</td><td>Line2Cell2</td><td>Line2Cell3</td>
                    </tr>
                    <tr>
                        <td>Line3Cell1</td><td>Line3Cell2</td><td>Line3Cell3</td>
                    </tr>
                    <tr>
                        <td>Line4Cell1</td><td>Line4Cell2</td><td>Line4Cell3</td>
                    </tr>
                    <tr>
                        <td>Line5Cell1</td><td>Line5Cell2</td><td>Line5Cell3</td>
                    </tr>
                    <tr>
                        <td>Line6Cell1</td><td>Line6Cell2</td><td>Line6Cell3</td>
                    </tr>
                    <tr>
                        <td>Line7Cell1</td><td>Line7Cell2</td><td>Line7Cell3</td>
                    </tr>
                    <tr>
                        <td>Line8Cell1</td><td>Line8Cell2</td><td>Line8Cell3</td>
                    </tr>
                    <tr>
                        <td>Line9Cell1</td><td>Line9Cell2</td><td>Line9Cell3</td>
                    </tr>
                    <tr>
                        <td>Line10Cell1</td><td>Line10Cell2</td><td>Line10Cell3</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</body>
</html>

【问题讨论】:

  • 请在此处至少包含一些您拥有的代码。
  • @CristianCiupitu 是的,我只是放了代码。
  • 您可以这样做,但宽度列可能随时中断。 jsfiddle.net/MVxZb/9

标签: javascript html css


【解决方案1】:

滚动不起作用的原因是您的 .innerDiv max-height 属性上没有任何单位。

为了使表头的宽度与表格相匹配,如果您可以通过 CSS 设置 &lt;th&gt;&lt;td&gt; 单元格的宽度,则不需要 javascript。

看到这个:http://jsfiddle.net/MVxZb/10/

编辑:更新链接。忘记删除不需要的javascript。

【讨论】:

  • 谢谢它很好用。还有一件事是有什么简单的方法可以将滚动条移动到表格主体附近?
  • 还有很多其他方法可以做到这一点,但如果我们只是采用您编写的代码并对其进行修改,您可以在 outerDiv 上设置一个等于 &lt;td&gt; 总和的宽度widths 然后将 innerDiv 设置为 width: 100%, something like this
【解决方案2】:

你的问题不是 javascript 是 html 您需要将标题和表格分开在 2 个不同的容器中 没有将标题分组在您的 tbody 的 sam 容器下,所以基本上您可以这样做

<div style="width:200px">
 <table style="width:100%">
     <tr><th>header</th></tr>
 </table>
<div>
<div style="width:200px; height:100px; overflow:auto">
    <table style="width:100%">
      <tr>
        <td>value</td>
      </tr>

    </table>
<div>

就像这里演示的那样

http://jsfiddle.net/MVxZb/12/

【讨论】:

    【解决方案3】:

    你需要给absolute位置中设置的元素一个width

    为了更好看,宽度应小约 1 em(滚动条的平均宽度)。

    如果所有列的宽度均等,您可以执行以下操作:http://jsfiddle.net/MVxZb/9/

    .outerDIV {
        position: relative;
        padding-top: 30px;
        display:inline-block;
    }
    .innerDIV {
        overflow: auto;
        max-height: 150px;
        padding-right:1.1em;
        margin-right:-1.1em;
    }
    table thead {
    display:table;
        width:100%;
        border:solid 1px gray;
        box-sizing:border-box;
        position: absolute;
        border-bottom:0;
        top: 0;
        left: 0;
        right:0;
    }
    

    如果 th 和 tds 没有设置宽度,您可以使用 table-layout:fixed; 将宽度均匀分布到您的列。

    【讨论】:

      【解决方案4】:

      每当我需要固定表格的标题时,我都会使用它。我也很感激其他解决方案。

      这里是fiddle

      table th,
      table td {
          padding: 10px;
          width: 100px;
      }
      
      .fixed-header{
          position: fixed;
          background: #fff;
      }
      .container{
          height: 402px;
          overflow: auto;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-19
        • 2018-02-26
        • 2021-12-25
        相关资源
        最近更新 更多