I'm currently working on a basic CRUD data table and I have added a refresh function using AJAX to dynamically append HTML without reloading the page. Everything was working fine until I introduced pagination from Laravel. The issue now is that only the first 5 data entries are displayed on each subsequent page.
$(document).ready(function() {
// Function to refresh table data
function refreshTable(page) {
$.ajax({
url: '/get-latest-projects?page=' + page, // URL to fetch updated data
method: 'GET',
success: function(response) {
// Update table with new data
$('#dataTable tbody').html(' '); // Assuming data is in HTML format for the table body only
$.each(response.data, function(index, item) {
var row = '<tr class="tr">';
row += '<td>' + item.input_date + '</td>';
row += '<td>' + item.nama_project + '</td>';
row += '<td class="desc">' + item.requestor + '</td>';
row += '<td>' + item.category_project + '</td>';
row += '<td><span>' + item.description_project + '</span></td>';
row += '<td>' + item.status + '</td>';
row += '<td><span class="status--process">' + item.pic_project + '</span></td>';
row += '<td>' + item.eta_project + '</td>';
row += '<td><div class="table-data-feature" id="editContainer">';
row += '<button type="button" class="item" data-toggle="modal" data-target="#editModal' + item.id + '" data-placement="top" title="Edit"><i class="zmdi zmdi-edit"></i></button>';
row += '<button class="item" data-toggle="tooltip" data-placement="top" title="Delete"><i class="zmdi zmdi-delete"></i></button>';
row += '</div></td></tr>';
$('#dataTable tbody').append(row);
});
},
error: function(xhr, status, error) {
console.error('Error refreshing table:', error);
}
});
}
refreshTable(1);
// Reload table when the button is clicked
$('#reloadButton').click(function() {
refreshTable(1);
});
});
The table briefly displays the next page's data before being overwritten by newly fetched data from the appended HTML content.
public function index()
{
$projects = Project::orderBy('id', 'desc')->paginate(5);
return view('table', compact('projects'));
}
public function getLatestProjects()
{
$projects = Project::orderBy('id', 'desc')->latest()->paginate(5); // Adjust as needed
return response()->json($projects);
}
This attempt aims to address the issue of an undefined object experienced previously.