You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为PHP生成的表格添加Bootstrap Table排序、搜索及分页功能

Fixing Bootstrap Table Sort, Search & Pagination for Your PHP-Generated Table

I’ve run into this exact issue before — let’s break down what’s missing and fix it step by step. The problem usually boils down to incorrect dependency loading, missing table structure requirements, or misconfigured initialization.

Step 1: Verify Correct Dependency Order

Bootstrap Table relies on jQuery, Bootstrap’s core files, and its own assets. Make sure you load them in this exact order in your <head> or right before </body>:

<!-- jQuery (required for Bootstrap and Bootstrap Table) -->
<script src="jquery.min.js"></script>

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="bootstrap.min.css">

<!-- Bootstrap Table CSS -->
<link rel="stylesheet" href="bootstrap-table.min.css">

<!-- Bootstrap JS -->
<script src="bootstrap.min.js"></script>

<!-- Bootstrap Table JS -->
<script src="bootstrap-table.min.js"></script>

Note: Replace the filenames with the actual paths to your downloaded files (the order stays critical regardless of local vs CDN hosting).

Step 2: Fix Your PHP-Generated Table Structure

Bootstrap Table needs proper <thead>/<tbody> tags and specific data attributes to enable features. Here’s the corrected PHP code:

<div class="data-content">
  <span>REPORT SELECTION</span>
  <div class="data-table" id="tableData">
    <?php 
    $qry = "select id, name, identity, created, payOption, amount, receivedBy from payment"; 
    $result = mysqli_query($conn, $qry); 
    if ($result->num_rows > 0) { 
        // Add Bootstrap Table initialization attributes
        echo "<table id='dataTable' data-toggle='table' data-search='true' data-pagination='true' data-sortable='true'>";
        // Wrap header in <thead> for proper feature detection
        echo "<thead><tr>
                <th data-field='name' data-sortable='true'>Payer</th>
                <th data-field='identity' data-sortable='true'>Passport/IC No</th>
                <th data-field='created' data-sortable='true'>Date Paid</th>
                <th data-field='payOption' data-sortable='true'>Payment Type</th>
                <th data-field='amount' data-sortable='true'>Payment Amount</th>
                <th data-field='receivedBy' data-sortable='true'>Received by</th>
                <th data-field='receipt'>Payment Receipt</th>
              </tr></thead>";
        // Wrap data rows in <tbody>
        echo "<tbody>";
        while($row = $result->fetch_assoc()) { 
            // Fix ID attribute quote syntax to avoid HTML errors
            echo "<tr id='".$row["id"]."'>
                    <td>".$row["name"]."</td>
                    <td>".$row["identity"]."</td>
                    <td>".$row["created"]."</td>
                    <td>".$row["payOption"]."</td>
                    <td>".$row["amount"]."</td>
                    <td>".$row["receivedBy"]."</td>
                    <td><a href='invoice.php' onclick='sessionFunction(".$row["id"].")'>View</a></td>
                  </tr>"; 
        } 
        echo "</tbody></table>"; 
    } else { 
        echo "<p>No payments found.</p>";
    } 
    ?>
  </div>
</div>

Key fixes here:

  • Added data-toggle="table" to auto-initialize Bootstrap Table without extra JS
  • Enabled search, pagination, and sorting globally with data-search="true", data-pagination="true", data-sortable="true"
  • Wrapped headers in <thead> and rows in <tbody> (critical for Bootstrap Table to identify content sections)
  • Fixed unescaped double quotes in the <tr> ID attribute (prevents broken HTML)
  • Added data-field to each header to map column data for sorting/search functionality

Step 3: Optional JS Initialization (For More Control)

If you prefer to initialize via JavaScript instead of data attributes, remove the data-toggle="table" attribute from the table and add this script right before </body>:

$(document).ready(function() {
  $('#dataTable').bootstrapTable({
    search: true,
    pagination: true,
    sortable: true,
    columns: [
      { field: 'name', sortable: true, title: 'Payer' },
      { field: 'identity', sortable: true, title: 'Passport/IC No' },
      { field: 'created', sortable: true, title: 'Date Paid' },
      { field: 'payOption', sortable: true, title: 'Payment Type' },
      { field: 'amount', sortable: true, title: 'Payment Amount' },
      { field: 'receivedBy', sortable: true, title: 'Received by' },
      { field: 'receipt', title: 'Payment Receipt' }
    ]
  });
});

Common Pitfalls to Double-Check

  • Dependency Order: If jQuery loads after Bootstrap/Bootstrap Table, none of the features will work. Always load jQuery first.
  • HTML Syntax Errors: Your original code had unescaped double quotes in the <tr> ID, which would invalidate the HTML and break table targeting.
  • Missing <thead>/<tbody>: Bootstrap Table uses these tags to distinguish headers from data rows — without them, sorting/search won’t function.
  • File Paths: Use your browser’s dev tools (Network tab) to confirm all CSS/JS files are loading correctly (no 404 errors).

Once you implement these changes, your table should have fully working sorting, search, and pagination!

内容的提问来源于stack exchange,提问作者user12358450

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 19:47:40