如何在不使用jQuery的情况下使用DataTable?解决DataTable未定义报错问题
Hey there, let's fix that DataTable is not defined error you're seeing. The core issue here is that you're probably using the jQuery-dependent version of DataTables, but you want the vanilla JavaScript build instead. Here's how to get it working properly:
Step 1: Load the Correct Vanilla JS DataTables Resources
First, make sure you're including the non-jQuery DataTables files in your HTML. Skip the jquery.dataTables.min.js file entirely—instead use the standalone vanilla version:
<!-- DataTables CSS --> <link rel="stylesheet" href="https://cdn.datatables.net/1.13.7/css/dataTables.dataTables.min.css"> <!-- Vanilla JS DataTables (no jQuery required) --> <script src="https://cdn.datatables.net/1.13.7/js/dataTables.min.js"></script>
Double-check that you're not accidentally loading jQuery anywhere else if you truly want a jQuery-free setup, but even if you do, the vanilla build will still work without relying on it.
Step 2: Verify Your Table Structure
DataTables requires a properly formatted HTML table to initialize correctly. Make sure your orderDataTemplate renders a table with:
- A
<table>tag (with theid="order_information"you're targeting) - A
<thead>section with column headers - A
<tbody>section for your data rows
Example valid structure:
<table id="order_information" class="display"> <thead> <tr> <th>Product ID</th> <th>Name</th> <th>Price</th> </tr> </thead> <tbody> <!-- Your dynamic data rows go here --> </tbody> </table>
Step 3: Tweak Your Initialization Code
Your current initialization line new DataTable("#order_information"); is correct for the vanilla build, but we can make it more robust and add error handling for edge cases:
(function () { const getOrderInformationData = async (container) => { try { const response = await mdcRequestData("/product/information-table/go2wire"); if (response && response.data.length > 0) { container.innerHTML = mdcProcessData(response.data, "orderDataTemplate"); // Grab the table element directly from the container instead of relying on global ID const tableElement = container.querySelector("#order_information"); if (tableElement) { new DataTable(tableElement); } else { container.innerHTML = '<div class="alert alert-danger" role="alert">Error: Table structure missing from template.</div>'; } } else { container.innerHTML = '<div class="alert alert-danger" role="alert">No product information was found.</div>'; } } catch (error) { console.error("Failed to fetch product data:", error); container.innerHTML = '<div class="alert alert-danger" role="alert">Failed to load product information. Please try again later.</div>'; } }; const orderDataContainer = document.getElementById("order_information"); if (orderDataContainer) { getOrderInformationData(orderDataContainer); } })();
Key Improvements:
- Added
try/catchto handle network errors or failed requests (prevents uncaught promise errors) - Targets the table element directly from the container instead of using a global ID (safer if there are multiple tables on the page)
- Adds fallback messages for cases where the template doesn't render a valid table
Common Pitfalls to Avoid:
- Using the wrong JS file: If you load
jquery.dataTables.min.js, theDataTableglobal won't exist—you'd have to use$(selector).DataTable()which requires jQuery. Stick todataTables.min.jsfor the vanilla version. - Invalid table structure: Missing
<thead>or<tbody>will cause DataTables to fail silently or throw errors. - Initializing before the table exists: Your code avoids this by rendering the HTML first, but always double-check that the table is present in the DOM before calling
new DataTable().
内容的提问来源于stack exchange,提问作者abu abu




