如何不刷新页面加载新Dynatable?Laravel下AJAX加载方案问询
Absolutely! You can absolutely load and refresh a Dynatable using AJAX in your Laravel Blade setup. Let’s break down how to fix up your current code and get this working smoothly.
Step 1: Fix & Complete Your AJAX Function
First, let’s finish the success callback in your adminXconnectAjax function, add error handling, and include logic to update Dynatable:
function adminXconnectAjax(value) { let newValue; // Add error handling for JSON parsing to avoid crashes try { newValue = JSON.parse(value); } catch (e) { console.error("Invalid JSON input:", value); return; } console.log("AJAX route hit, waiting for response"); $.ajax({ type: 'GET', url: '/xconnectadmin/', data: { xconnect: newValue }, dataType: 'json', success: function(data) { console.log("Response received:", data); // Option 1: Replace the entire table with a rendered Blade partial // (Great if you want to keep table structure logic in Blade) $('#dynatable-container').html(data.tableHtml); // Re-initialize Dynatable on the new table $('#dynatable-table').dynatable({ // Match your initial Dynatable configuration here dataset: { records: data.tableData } }); // Option 2: Update existing Dynatable with new data // (Faster if your table structure doesn't change) // const dynatable = $('#dynatable-table').data('dynatable'); // dynatable.settings.dataset.originalRecords = data.tableData; // dynatable.process(); }, error: function(xhr, status, error) { // Critical for debugging AJAX issues console.error("AJAX Request Failed:", error); console.log("Server Response:", xhr.responseText); } }); }
Step 2: Update Your Laravel Backend Route
Make sure your /xconnectadmin/ route returns the right data—either raw records or a rendered Blade table partial. Here’s how to set that up in routes/web.php:
use Illuminate\Http\Request; use App\Models\YourXconnectModel; // Replace with your actual model Route::get('/xconnectadmin/', function(Request $request) { // Retrieve the xconnect parameter from the AJAX request $xconnectParams = $request->input('xconnect'); // Fetch your data (adjust this query to match your use case) $tableData = YourXconnectModel::where('some_column', $xconnectParams)->get(); // Return JSON with both raw data and rendered table HTML return response()->json([ 'tableData' => $tableData, 'tableHtml' => view('partials.dynatable-table', ['data' => $tableData])->render() ]); });
Step 3: Prepare Your Blade Template
Ensure your Blade file has a container for the Dynatable, and initialize it on page load:
<!-- Dynatable Container --> <div id="dynatable-container"> <table id="dynatable-table" class="table"> <thead> <tr> <!-- Add your table headers here --> <th data-dynatable-column="id">ID</th> <th data-dynatable-column="name">Name</th> <!-- More columns as needed --> </tr> </thead> <tbody> <!-- Initial rows (or empty if loading via AJAX on first load) --> </tbody> </table> </div> <script> // Initialize Dynatable on page load $(document).ready(function() { $('#dynatable-table').dynatable({ dataset: { records: [] // Start with empty data, or load default records here } }); }); </script>
Key Notes to Avoid Issues
- CSRF Protection: Since you’re using a
GETrequest, CSRF tokens aren’t required, but if you switch toPOSTlater, add the token to your request headers or form. - Dynatable Reinitialization: If you replace the entire table HTML, you must re-run
dynatable()on the new table element. - Data Format: Ensure your backend returns data in a format Dynatable expects (plain objects/arrays, not nested structures unless you configure Dynatable to handle them).
Give these steps a try—if you run into specific snags (like Dynatable not rendering, or data not populating), share more details about your table structure or model logic and we can troubleshoot further!
内容的提问来源于stack exchange,提问作者Brad Goldsmith

