Hockey Raxoi Seguintes partidos
* Horarios recollidos da web da [F.G.Patinaxe](http://competiciones.fgpatinaxe.gal/).
https://code.jquery.com/jquery-3.6.0.min.js
$(document).ready(function () {
let URL = "https://ns3104249.ip-54-37-85.eu/shared/portales_files/agenda_portales.php?cliente=fgpatinaxe&idm=1&id_temp=31";
let FILTER_ONLY = "RAXOI";
const days = ['Domingo', 'Luns', 'Martes', 'Mércores', 'Xoves', 'Venres', 'Sábado'];
const date = new Date();
const categoryOrder = ["MICRO", "PREBENJA", "BENJA", "ALEV", "INFANTIL", "SUB 15"];
function replace_name_in_cells(){
const targetString = 'HOCKEY CLUB RAXOI';
const newString = 'HC RAXOI';
const cells = document.querySelectorAll('td');
cells.forEach(cell => {
const originalContent = cell.textContent;
cell.textContent = cell.textContent.replace(new RegExp(targetString, 'g'), newString);
if (originalContent !== cell.textContent) {
cell.classList.add('important');
}
});
};
$.get(URL, function (data) {
// Find table with id "agenda_tabla" in the response
var $table = $(data).find("#agenda_tabla");
$table.find('tr:not(:contains('+FILTER_ONLY+'))').remove(); // Filter table rows that do not contain "RAXOI" string
// Create an object to store the rows grouped by category
var groups = {};
// Iterate over each row in the table, grouping them by category
$table.find("tr").each(function () {
// Get the category of the row
var category = $(this).find("td:eq(1)").text().trim();
// If the category does not exist in the groups object, create a new array
if (!groups[category]) {
groups[category] = [];
}
// Add the row to the corresponding category array
groups[category].push($(this));
// Working with dates
var dateText = $(this).find("td:eq(2)").text().trim();
var dateArr = dateText.split("/");
var matchDate = new Date(dateArr[2], dateArr[1] - 1, dateArr[0]); // Parse the match date stored in the row
const dayName = days[matchDate.getDay()]
$(this).find("td:eq(2)").text(dayName.substring(0,3)+" "+dateText);
if (
matchDate.getTime() - Date.now() < 864000000 // Check if the match is within 10 days
) {
$(this).addClass("match-soon"); // Add class "match-soon" to the row
}
});
// Custom sort function to sort by category start matching
function customSort(a, b) {
for (var i = 0; i < categoryOrder.length; i++) {
var startA = a.startsWith(categoryOrder[i]);
var startB = b.startsWith(categoryOrder[i]);
if (startA && startB) {
return 0;
//return categoryOrder.indexOf(a) - categoryOrder.indexOf(b);
} else if (startA) {
return -1;
} else if (startB) {
return 1;
}
}
return a.localeCompare(b);
}
// Sort the groups object by category following the custom sort function
var sortedGroups = {};
Object.keys(groups).sort(customSort).forEach(function(key) {
sortedGroups[key] = groups[key];
});
// Clear the original table and append the grouped rows with category separators
$table.empty();
$.each(sortedGroups, function (category, rows) {
// Create a separator row with the category as the text content
var $separator = $(
'
' + category + "
"
);
// Remove firsts columns from table
$.each(rows, function() {
$(this).find('td:nth-child(2)').remove(); // Remove the second column
$(this).find('td:nth-child(1)').remove(); // Remove the first column
$(this).find('td:nth-child(5)').remove();
$(this).find('td:nth-child(6)').remove();
});
// Append the separator row and the rows of the category to the table
$table.append($separator).append(rows);
});
// Append the filtered table to an element with id "result" in the HTML page
$("#result").append($table);
replace_name_in_cells();
}); // $.get
});
/ Center content vertically and horizontally /
body {
margin-top: 5px;
display: flex;
align-items: center;
justify-content: center;
background-color: #EEE;
}
.center {
margin-top: 50px;
background-color: #FFF;
padding: 20px;
border-radius: 10px;
border-style: solid;
}
.note {
padding: 20px;
font-size: small;
}
.important {
font-weight: bold;
}
.category-sep {
padding: 5px;
font-size: medium;
text-align: left;
background-color: #DDD;
font-weight: bold;
}
.match-soon {
text-align: left;
background-color: #ffdab3;
}