function addOrUpdateUrlParameter(url, key, value) { var urlObject = new URL(url); urlObject.searchParams.set(key, value); return urlObject.href; } function getUrlParameters() { var searchParams = new URLSearchParams(window.location.search); var params = {}; // Iterate over each parameter and add to the 'params' object searchParams.forEach(function(value, key) { params[key] = value; }); return params; } $(document).ready(function() { var params = getUrlParameters(); // Make table rows clickable $(".table-wrapper tbody tr").click(function() { var url = $(this).data("href"); if (url) { window.open(url, '_blank'); } }); $(".th-desc").hide(); $(".th-asc").hide(); if ('order_by' in params) { if ('order_type' in params && params.order_type == 'desc') { $('th:contains(' + params.order_by + ') .th-desc').show(); } else { $('th:contains(' + params.order_by + ') .th-asc').show(); } } // Make table rows clickable $(".table-wrapper thead th").click(function(e) { var column_name = $(this).find('span:first').text(); var newUrl = addOrUpdateUrlParameter(window.location.href, 'order_by', column_name); if ('order_by' in params && 'order_type' in params && params.order_by == column_name && params.order_type == 'desc') { newUrl = addOrUpdateUrlParameter(newUrl, 'order_type', 'asc'); } else { newUrl = addOrUpdateUrlParameter(newUrl, 'order_type', 'desc'); } // Redirect to the modified URL window.location.href = newUrl; }); });