-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtables.html
150 lines (127 loc) · 5.66 KB
/
tables.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Merged & Sorted Table</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
<script>
var removeAttr = jQuery.fn.removeAttr;
jQuery.fn.removeAttr = function() {
if (!arguments.length) {
this.each(function() {
// Looping attributes array in reverse direction to avoid skipping items
for (var i = this.attributes.length - 1; i >= 0; i--) {
jQuery(this).removeAttr(this.attributes[i].name);
}
});
return this;
}
return removeAttr.apply(this, arguments);
};
</script>
</head>
<body class="container mt-4">
<h2>Filtered & Sorted Table</h2>
<button onclick="fetchAndMergeTables()" class="btn btn-primary">Fetch Data</button>
<div id="filtered-results" class="mt-4">
<p>Click "Fetch Data" to load results.</p>
</div>
<script>
// Constants: Two URLs to fetch data from
const urls = [
"https://contaste.pro/eng/85724?schedule&part=109605", // Replace with actual URL 1
"https://contaste.pro/eng/85724?schedule&part=109606" // Replace with actual URL 2
];
const searchTerm = "Vision, Beer Sheva";
async function fetchAndMergeTables() {
try {
$("button").prop("disabled", true).text("Processing...");
let allRows = [];
let rowSource = 0; // Variable to keep track of which URL the rows come from
// Fetch data from both URLs
for (const url of urls) {
const response = await fetch(url);
const htmlText = await response.text();
// Parse the HTML
const parser = new DOMParser();
const doc = parser.parseFromString(htmlText, "text/html");
// Find the first table (adjust selector if needed)
const table = doc.querySelector("table");
if (!table) continue;
// Filter rows
const rows = Array.from(table.querySelectorAll("tr")).filter(row =>
row.textContent.includes(searchTerm)
);
// Add rows to allRows array and mark rows from the second URL
rows.forEach((row, index) => {
if (rowSource === 1) {
row.className = "table-primary"; // Replace current classes with "table-primary" for rows from the second URL
}
allRows.push(row);
});
rowSource++; // Increment to mark the next URL's rows
}
// Sort rows by time (3rd <td> which contains <time>)
allRows.sort((rowA, rowB) => {
const timeA = getTimeFromRow(rowA);
const timeB = getTimeFromRow(rowB);
return timeA - timeB;
});
// Display the results
displayResults(allRows);
} catch (error) {
console.error("Error fetching data:", error);
document.getElementById("filtered-results").innerHTML = `<p>Error fetching data.</p>`;
} finally {
// Re-enable the button after processing
$("button").prop("disabled", false).text("Fetch Data"); // Restore the button text
}
}
function getTimeFromRow(row) {
const columns = row.querySelectorAll("td");
if (columns.length < 3) return 0; // Ensure there's a 3rd column
const timeElement = columns[2].querySelector("time"); // Extract <time> element
if (!timeElement) return 0; // Fallback if no <time> tag is found
const timeText = timeElement.textContent.trim();
const [hours, minutes] = timeText.split(":").map(Number);
if (isNaN(hours) || isNaN(minutes)) return 0; // Fallback if time is invalid
return new Date(1970, 0, 1, hours, minutes).getTime(); // Convert to timestamp
}
function displayResults(rows) {
const resultsContainer = document.getElementById("filtered-results");
if (rows.length === 0) {
resultsContainer.innerHTML = "<p>No matching rows found.</p>";
return;
}
// Create a new table with sorted & merged rows
const tableHTML = `
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Start</th>
<th scope="col">Category</th>
<th scope="col">Studio</th>
<th scope="col">Group Name</th>
</tr>
</thead>
<tbody>
${rows.map(row => row.outerHTML).join("")}
</tbody>
</table>
`;
resultsContainer.innerHTML = tableHTML;
// Remove the second column and all attributes from rows and cells
$("table tbody tr").each(function() {
$(this).find("td:nth-child(2)").remove(); // Remove the second <td> in each row
//$(this).removeAttr(); // Remove all attributes from the <tr> element
});
$("table tbody td").each(function() {
$(this).removeAttr(); // Remove all attributes from <td> elements
});
}
</script>
</body>
</html>