doudou5023 2016-10-19 13:27
浏览 71
已采纳

Bootstrap dataTable没有在php表中排序和搜索

I'm a site that has one controller page to run the mysql query. It outputs the results to a viewer page named m_analitica.php, with include("header.php") at top and include("footer") at the bottom.

code for m_analitica.php :

<div>
<table class="table table-striped table-bordered table-hover" id="myTable">
    <div><h2>Mapa Analítica</h2></div>
    <thead>
        <tr>
            <th>Name</th>
            <th>Tipo Documento</th>
            <th>Número</th>
            <th>Cliente</th>
            <th>Obra</th>
            <th>Designação</th>
            <th>Agregado</th>
            <th>Quantidade (ton)</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>Name</th>
            <th>Tipo Documento</th>
            <th>Número</th>
            <th>Cliente</th>
            <th>Obra</th>
            <th>Designação</th>
            <th>Agregado</th>
            <th>Quantidade (ton)</th>
        </tr>
    </tfoot>


    <?php foreach ($positions as $position): ?>
        <tbody>
            <tr>
                <td><?= $position["data"] ?></td>
                <td><?= $position["tipo"] ?></td>
                <td><?= $position["num"] ?></td>
                <td><?= $position["cliente"] ?></td>
                <td><?= $position["obra"] ?></td>
                <td><?= $position["nome_obra"] ?></td>
                <td><?= $position["agr"] ?></td>
                <td><?= $position["ton"] ?></td>
            </tr>
        </tbody>
    <?php endforeach ?>
</table>

In the head of header.php I included the following js scripts and CSS:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" href="../public/js/DataTables-1.10.12/media/css/dataTables.bootstrap.min.css" />
<link href="../public/css/bootstrap.min.css" rel="stylesheet"/>
<script src="../public/js/bootstrap.min.js"></script>
<script src="../public/js/DataTables-1.10.12/media/js/jquery.dataTables.min.js"></script>
<script src="../public/js/DataTables-1.10.12/media/js/dataTables.bootstrap.min.js"></script>

Then before body element closure in footer.php I inserted the script:

<script type="text/javascript">
    $('#myTable').dataTable();
</script>

I get the formatting right, except it does not divide the table into pages, and the search and sorting are not working, it does nothing by clicking them.

Does the bootstrap dataTable not work with php? Or with foreach loops? Or his there something wrong with the code.

  • 写回答

1条回答 默认 最新

  • douchen2025 2016-10-19 13:30
    关注

    Remove the <tbody> tags from inside your loop. You only want to repeat the <tr> tags. Put the <tbody> tags outside of the loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题