【问题标题】:DataTables pagination and search not working数据表分页和搜索不起作用
【发布时间】:2017-01-22 21:31:00
【问题描述】:

我有一个显示来自数据库的数据的 DataTable。它使用 while 循环来获取数据并显示在表中。但我现在遇到的问题是,数据库中的所有数据行都出现在表中,没有分页。所以假设我在数据库中有 100 行数据,所有数据都出现在一个长表中,我必须继续向下滚动才能查看数据库中的所有数据。这意味着每页显示的记录和分页不起作用。搜索和排序方面也不起作用。以下是我的代码和数据表的屏幕截图。请帮助我,因为我很累。

<table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                <thead>
                                    <tr>
                                        <th>Event Date</th>
                                        <th>number</th>
                                        <th>Agent number</th>
                                        <th>Agent Name</th>
                                        <th>Remarks</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php

                                $result = mysqli_query($conn,"SELECT * FROM roaming");
                                $count = mysqli_num_rows($result);

                                while($row=mysqli_fetch_array($result)){

                                ?>


                                    <tr class="">
                                        <td><?php echo $row['eventDate'];?></td>
                                        <td><?php echo $row['number'];?></td>
                                        <td><?php echo $row['agent_number'];?></td>
                                        <td class="center"><?php echo $row['service'];?></td>
                                        <td class="center"><?php echo $row['vpmn'];?></td>
                                    </tr>
                                    <?php   } //end of while loop ?>
                                </tbody>
                            </table>

数据表截图

【问题讨论】:

  • 我没有看到您的 javascript 数据表正在初始化。此外,您的代码会为每个结果输出一个新的 &lt;tbody&gt;,这不一定是可取的。
  • 谢谢@Blake。将 &lt;tbody&gt; 放在 while 循环之前,以免为每个结果输出新的 解决了我的问题。
  • 你可以直接关闭这个问题,因为它只是一个格式错误,不会有太多的未来价值。

标签: php jquery mysql datatable pagination


【解决方案1】:

例如,您的 SQL 查询“SELECT * FROM roaming”会返回所有行,而不仅仅是前 10 行。

要获取前十行,您的 SQL 查询应该是这样的:

"SELECT * FROM roaming LIMIT 0,10"

要获取从 10 到 20 的行(用于下一页),请使用如下查询:

"SELECT * FROM roaming LIMIT 10,10"

【讨论】:

  • Datatables 是一个 JavaScript 库。如果他设置了服务器端处理,您的建议可能适用,而他根本没有提到。
猜你喜欢
相关资源
最近更新 更多
热门标签