// Function to load data based on the selected value
function loadRanking(courseId, pageNumber = 1) {
// Make a GET request to the server
$.ajax({
url: "/game/idac/ranking/ranking.get",
type: "GET",
data: { courseId: courseId, pageNumber: pageNumber },
dataType: "json",
success: function (data) {
// check if an error inside the json exists
if (!data.success) {
// Inject the table into the container
$("#table-ranking").html("
" + data.error + "
");
console.error("Error: " + data.error);
return;
}
// get the total number of pages
var total_pages = data.total_pages;
// Generate the HTML table
var tableHtml = '
#
Name/Car
Time
Eval
Store/Date
';
$.each(data.ranking, function (i, ranking) {
// Add a 1 to the i variable to get the correct rank number
tableHtml += `
';
// Ignore the Store and Date columns on small screens
tableHtml += '
' + ranking.store + ' ' + ranking.update_date + '
';
tableHtml += '
';
});
tableHtml += '
';
// Inject the table into the container
$("#table-ranking").html(tableHtml);
// Generate the Pagination HTML
var paginationHtml = '';
// Inject the pagination into the container
$("#pagination-ranking").html(paginationHtml);
},
error: function (jqXHR, textStatus, errorThrown) {
// Inject the table into the container
$("#table-ranking").html("
" + textStatus + "
");
console.error("Error: " + textStatus, errorThrown);
}
});
}
// Function to handle page changes
function changePage(pageNumber) {
// Get the selected value
var courseId = $("#course-select").val();
// Call the function to load data with the new page number
loadRanking(courseId, pageNumber);
}
$(document).ready(function () {
// Attach an event handler to the select element
$("#course-select").change(function () {
// Get the selected value
var courseId = $(this).val();
// Call the function to load data
loadRanking(courseId);
});
// Event delegation for pagination links
$("#pagination-ranking").on("click", "a.page-link", function (event) {
// event.preventDefault(); // Prevent the default behavior of the link
var clickedPage = $(this).data("page");
// Check if the changePage function is not already in progress
if (!$(this).hasClass('disabled')) {
// Handle the page change here
changePage(clickedPage);
}
});
});