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

基于Google Sheet的HTML表格搜索与大数据分页加载方案问询

Solution: Add Search & Efficient Deferred Loading to Your Google Sheets Table

Got it, let's get your table set up with search functionality, pagination, and efficient deferred loading (just like the example you referenced) to handle large datasets up to 50k rows. Here's how to do it step by step:


Step 1: Add DataTables Resources

First, include the DataTables CSS and JavaScript files in your HTML. These will handle all the heavy lifting for search, pagination, and deferred rendering.

Add this CSS in the <head> section of your page:

<link rel="stylesheet" href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css">

Add this JavaScript right before the closing </body> tag (after jQuery, since DataTables depends on it):

<script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>

Step 2: Update Your HTML & JavaScript

Replace your existing table script with this modified version. Instead of manually appending every row (which is slow for large datasets), we'll feed the data to DataTables, which will only render rows as needed.

Here's the full updated code:

<p class="font-weight-bold">The data in the table comes from the google spreadsheet</p>
<a class="btn btn-primary mb-4" href="https://docs.google.com/spreadsheets/d/1t2_HHLkibAybPORmXuDDEFEyetC3p7r1blQRpjzinXg/edit?usp=sharing" role="button" target="_blank">Click to see the source sheet</a>

<!-- Table -->
<table class="table table-bordered table-striped" id="testTable">
  <!-- Table head -->
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>E-mail</th>
    </tr>
  </thead>
  <!-- Table body (DataTables will populate this) -->
  <tbody></tbody>
</table>
<!-- Table -->

<script>
$(document).ready(function() {
  // Fetch data from Google Sheets
  $.getJSON("https://spreadsheets.google.com/feeds/list/1t2_HHLkibAybPORmXuDDEFEyetC3p7r1blQRpjzinXg/od6/public/values?alt=json", function(data) {
    // Convert Google Sheets' JSON format into a simple array DataTables understands
    var tableData = data.feed.entry.map(function(entry) {
      return [
        entry['gsx$_cn6ca']['$t'], // Extract Name
        entry['gsx$_cokwr']['$t'], // Extract Age
        entry['gsx$_cpzh4']['$t']  // Extract E-mail
      ];
    });

    // Initialize DataTables with key features
    $("#testTable").DataTable({
      data: tableData,
      deferRender: true, // Critical for large datasets: only renders visible rows
      paging: true,      // Enable pagination to break data into pages
      searching: true,   // Add a search bar to filter rows
      ordering: true,    // Allow users to sort columns by clicking headers
      pageLength: 10,    // Number of rows to show per page (adjust as needed)
      responsive: true   // Optional: make table mobile-friendly
    });
  });
});
</script>

Key Features Explained

  • Deferred Rendering: The deferRender: true option tells DataTables to only create DOM elements for rows that are currently visible on the screen. This drastically improves performance with 50k+ rows, as it avoids rendering every row upfront.
  • Search Functionality: DataTables automatically adds a search bar at the top of the table that filters results in real-time.
  • Pagination: The table is split into pages, so users only load a small subset of data at a time.
  • Sorting: Clicking any column header will sort the data by that column (ascending/descending).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:56:03