gpt4 book ai didi

JavaScript 数据表 : remove unwanted horizontal scrolling

转载 作者:行者123 更新时间:2023-12-02 20:51:00 25 4
gpt4 key购买 nike

我正在使用DataTables显示我的数据。我将其宽度指定为 4 个引导列。尽管所有数据都可以适合指定的宽度,但我的表格现在在底部出现了不需要的水平滚动:

enter image description here

向右滚动,我看到顶部的搜索框是这次滚动的原因:

enter image description here

是什么让盒子位于太右侧?我该如何解决这个问题?

$(document).ready(function () {
var data = [
{ title: "The Godfather"},
{ title: "The Shawshank Redemption"},
{ title: "The Lord of the Rings: The Return of the King"},
{ title: "The Godfather: Part II"},
{ title: "Shichinin no samurai"},
{ title: "Buono, il brutto, il cattivo, Il"},
{ title: "Casablanca"},
{ title: "The Lord of the Rings: The Fellowship of the Ring"},
{ title: "The Lord of the Rings: The Two Towers"},
{ title: "Pulp Fiction"}
];


var table = d3.select("#myTable");
var rows = table
.select('tbody')
.selectAll('tr')
.data(data)
.enter()
.append('tr');

var cells = rows
.selectAll('td')
.data(function (data_row) {
return [data_row['title']];
})
.enter()
.append('td')
.text(function (d) {
return d;
});

$('#myTable').DataTable({
scrollY: '60vh',
paging : false,
});
});
body {
padding-top: 1%;
padding-bottom: 1%;
background: #585858 !important;
color: white;
}
<!DOCTYPE html>

<head>
<title>Data Tabke</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- font awesome lib -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/v4-shims.css">
<!-- bootstrap -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<!-- dataTable -->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap4.min.css">

<link rel="stylesheet" type="text/css" href="css/style.css">
</head>

<body>

<div class="container">
<div class="row">
<div id="FilterableTable" class="col-4">
<div class="table-responsive">
<table class="table table-striped table-dark table-hover table-fit" id="myTable">
<thead class='thead-dark'>
<tr>
<th>My data</th>

</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
<div id="FilterableTable2" class="col-8">
<div class="table-responsive">
<table class="table table-striped table-dark table-hover table-fit" id="otherTable">
<thead class='thead-dark'>
<tr>
<th>Col 1</th>
<th>Col 2</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>

</div>
</div>


<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.5.0.min.js"
integrity="sha256-xNzN2a4ltkB44Mc/Jz3pT4iU1cmeR0FkXs4pru/JxaQ="
crossorigin="anonymous"></script>
<!-- Popper.js -->
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"></script>
<!-- dataTable -->
<script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" charset="utf8"
src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js"></script>
<!-- D3 -->
<script src="https://d3js.org/d3.v3.min.js"></script>

</body>

使用代码片段更新。

最佳答案

我找到了一种方法来解决这个问题,使用 DOM positioning DataTables 本身。我需要将表的结构指定为 <f>ti初始化表时:

$('#myTable').DataTable({
scrollY: '60vh',
paging : false,
dom : "<f>ti"
});

然后将搜索框设置为向左浮动:

#myTable_filter {
float: left !important;
}

ID myTable_filter由 DataTables 自动生成,因此我需要检查 Chrome 中元素的正确 ID。

<f>ti 的说明位:

  • 首先,将搜索框( f 元素,或 Filtering ,根据 DataTables 文档)包装在带有 < 的 div 中。和> .
  • 将其放在表格之前,或 t元素。
  • 信息元素( i )位于最后。这是显示“显示 1 到 x,共 x 个条目”的信息行

关于JavaScript 数据表 : remove unwanted horizontal scrolling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61603666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com