我们如何使用固定标题固定滚动并调整标题宽度和td等于?

时间:2019-03-18 07:57:18

标签: javascript html css html-table

如果我添加n个标题,它将设置为一定的最小宽度,并且如果标题的总最小宽度必须大于表格宽度的100%,必须同时提供等于标题宽度的td宽度:< / p>

html代码如下: https://jsfiddle.net/1w7vnLec/

<html>
<head>
<style>
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: scroll;
}

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;
}

</style>




</head>
<body>
<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>
            <th>Head 6</th>
            <th>Head 7</th>
            <th>Head 8</th>
            <th>Head 9</th>
            <th>Head 10</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>
            <td>Content 5</td>
            <td>Content 5</td>
            <td>Content 5</td>
            <td>Content 5</td>
            <td>Content 5</td>
        </tr>
        <tr>
            <td>Content 1</td>
            <td>Content 1</td>
            <td>Lorem ipsum dolor sit amet.</td>
            <td>Lorem ipsum dolor sit amet.</td>
            <td>Lorem ipsum dolor sit amet.</td>
            <td>Lorem ipsum dolor sit amet.</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>
             <td>Content 5</td>
            <td>Content 5</td>
            <td>Content 5</td>
            <td>Content 5</td>
            <td>Content 5</td>
        </tr>

    </tbody>
</table>
</body>
</html>

我们如何使用固定标题固定滚动并调整标题宽度和td等于?

1 个答案:

答案 0 :(得分:1)

删除您的JS 并删除此CSS

val aa = ArrayAdapter<String>(requireActivity(), R.layout.simple_spinner_item, statuts.toList())

然后根据您要查找的行为,为您的table.scroll tbody, table.scroll thead { display: block; } 设置最小宽度,并将其父级设置为tdscroll: overflow scroll: auto

这是一个工作代码段:

overflow-x: auto
.scroll {
  overflow: scroll;
}

table {
     width: 100%; 
    border-spacing: 0;
    border: 2px solid black;
}

thead tr th { 
    height: 30px;
    line-height: 30px;
}

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

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

tbody td,
thead th {
    border-right: 1px solid black;
}

tbody td:last-child,
thead th:last-child {
    border-right: none;
}

tbody td {
    min-width: 200px;
}