javascript - 数据表分页不起作用?

标签 javascript jquery html pagination datatables

我的 html 页面包含一个表格。我使用 dataTable 插件进行分页。 1

1 https://datatables.net/examples/basic_init/alt_pagination.html

我的html如下。

<head>
<script src="https://code.jquery.com/jquery-1.12.4.js"
    type="text/javascript"></script>
<script type="text/javascript"
    src=" https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script type="text/javascript"
    src=" https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
  <style type="text/css">

table, th,td{
    border: 1px solid black;
    text-align:center;
}
#clients_data {
margin-bottom:100px;
}
</style> 
<meta charset="UTF-8">
<link rel="stylesheet"
    href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
<title>Clients</title>
</head>
<body>
    <table style="width: 100%" id="clients_data" class="display" >
        <caption>Clients</caption>
        <thead>
            <tr>
                <th>Clients</th>
                <th>Number of Sites</th>
                <th>Reset the Processing</th>
            </tr>
        </thead>
    </table>

    <table style="width: 100%" id="machines_data">
        <caption>Machines</caption>
        <thead>

            <tr>
                <th>Number</th>
                <th>Machine Name</th>
            </tr>
        </thead>
    </table>
    <script type="text/javascript">
        $(document).ready(function() {
            loadCustomers();
            loadMachines();

        });

        function loadCustomers() {
            $
                    .ajax({
                        type : 'GET',
                        url : 'http://localhost:8080/cache/getCustomers',
                        dataType : 'json',
                        success : function(data) {
                            var rows = [];
                            $
                                    .each(
                                            data,
                                            function(id, value) {
                                                rows
                                                        .push(' <tbody><tr><td><a href="clientSiteInfo.html?client='
                                                                + id
                                                                + '">'
                                                                + id
                                                                + '</td><td>'
                                                                + value
                                                                + '</td><td><button type="button" onclick="reset(\''
                                                                + id
                                                                + '\')">Reset</td></tr> </tbody>');
                                            });
                            $('#clients_data').append(rows.join(''));
                            $('#clients_data').DataTable({
                                "pagingType" : "full_numbers"
                            });

                        }
                    });
        };
.......

这会加载数据,但分页不起作用。意味着当我设置每页 10 个条目时,它会显示所有条目..我已附上屏幕截图。我还缺少其他插件吗? 但在提到的教程中,它说我只需要使用 "pagingType": "full_numbers" 属性.. enter image description here

最佳答案

分页按预期完美运行。问题是您错误地插入了 <tbody>每行的部分。因为你只能拥有一个 <tbody>每个数据表显示的分页将基于数据集中的第一行,因此始终显示总共一页。

你可以这样做:

rows
  .push(' <tr><td><a href="clientSiteInfo.html?client=' +
    id +
    '">' +
    id +
    '</td><td>' +
    value +
    '</td><td><button type="button" onclick="reset(\'' +
    id +
    '\')">Reset</td></tr> ');
});

$('#clients_data').append('<tbody>'+rows.join('')+'</tbody>');

但你真的应该考虑使用 columns 反而。

关于javascript - 数据表分页不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46780285/

相关文章:

javascript - 如何设置悬停时的 Bootstrap 下拉菜单?

html - 是否可以创建一个包含多行的粘性表头?

javascript - 虚拟 DOM 的比较算法是否能够找出唯一的差异并修补真实 DOM?

javascript - 使用 JavaScript 搜索的离线 HTML5 网站

javascript - jQuery:鼠标交互 + 简单动画 [img]

jquery - 如何: Live Validation with jQuery and Rails

php - HTML5 视频播放转换器

java - Primefaces 和 Twitter Bootstrap - Bootstrap 向导的样式

javascript - IE 警告解决方法?包含安全和不安全项目的页面

javascript - 如何在新的空白弹出窗口中打开pdf并下载