jabela

Fun toy with JS to search and order sheets

Aug 21st, 2024
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Exception Quiz</title>
  7.     <style>
  8.         table {
  9.             width: 100%;
  10.             border-collapse: collapse;
  11.         }
  12.         th, td {
  13.             padding: 8px;
  14.             text-align: left;
  15.             border: 1px solid #ddd;
  16.         }
  17.         th {
  18.             background-color: #f4f4f4;
  19.             cursor: pointer;
  20.         }
  21.         input[type="text"] {
  22.             width: 100%;
  23.             padding: 8px;
  24.             margin-bottom: 10px;
  25.             box-sizing: border-box;
  26.         }
  27.     </style>
  28. </head>
  29. <body>
  30. <h1>List of Links</h1>
  31. <input type="text" id="searchInput" placeholder="Search Links...">
  32. <table>
  33.     <thead>
  34.     <tr>
  35.         <th onclick="sortTable(0)">Title</th>
  36.         <th onclick="sortTable(1)">Description</th>
  37.         <th onclick="sortTable(2)">Link</th>
  38.     </tr>
  39.     </thead>
  40.     <tbody id="tableBody">
  41.     <!-- Data will be populated here -->
  42.     </tbody>
  43. </table>
  44.  
  45. <script>
  46.     const url = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vSqLkeyCxbcIBCnCEY4nIltdFjDAE4etzIhwh8BB3RKsZUoUWDDG_sm49ayjbWCRpG6mWIsCbICoSrR/pub?gid=1919940108&single=true&output=tsv';
  47.  
  48.     document.addEventListener('DOMContentLoaded', () => {
  49.         fetch(url)
  50.             .then(response => response.text())
  51.             .then(data => {
  52.                 const rows = data.trim().split('\n');
  53.                 const tableBody = document.getElementById('tableBody');
  54.                 rows.slice(1).forEach(row => {
  55.                     const cols = row.split('\t');
  56.                     const tr = document.createElement('tr');
  57.  
  58.                     cols.forEach((col, index) => {
  59.                         const td = document.createElement('td');
  60.                         if (index === 2) {
  61.                             const a = document.createElement('a');
  62.                             a.href = col;
  63.                             a.textContent = col;
  64.                             a.target = "_blank";
  65.                             td.appendChild(a);
  66.                         } else {
  67.                             td.textContent = col;
  68.                         }
  69.                         tr.appendChild(td);
  70.                     });
  71.  
  72.                     tableBody.appendChild(tr);
  73.                 });
  74.  
  75.                 document.getElementById('searchInput').addEventListener('input', filterTable);
  76.             });
  77.     });
  78.  
  79.     function filterTable() {
  80.         const input = document.getElementById('searchInput');
  81.         const filter = input.value.toLowerCase();
  82.         const table = document.querySelector('table');
  83.         const trs = table.getElementsByTagName('tr');
  84.  
  85.         for (let i = 1; i < trs.length; i++) {
  86.             trs[i].style.display = trs[i].innerText.toLowerCase().includes(filter) ? '' : 'none';
  87.         }
  88.     }
  89.  
  90.     function sortTable(n) {
  91.         const table = document.querySelector('table');
  92.         let rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
  93.         switching = true;
  94.         dir = "asc";
  95.  
  96.         while (switching) {
  97.             switching = false;
  98.             rows = table.rows;
  99.             for (i = 1; i < (rows.length - 1); i++) {
  100.                 shouldSwitch = false;
  101.                 x = rows[i].getElementsByTagName("TD")[n];
  102.                 y = rows[i + 1].getElementsByTagName("TD")[n];
  103.  
  104.                 if (dir == "asc") {
  105.                     if (x.textContent.toLowerCase() > y.textContent.toLowerCase()) {
  106.                         shouldSwitch = true;
  107.                         break;
  108.                     }
  109.                 } else if (dir == "desc") {
  110.                     if (x.textContent.toLowerCase() < y.textContent.toLowerCase()) {
  111.                         shouldSwitch = true;
  112.                         break;
  113.                     }
  114.                 }
  115.             }
  116.             if (shouldSwitch) {
  117.                 rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
  118.                 switching = true;
  119.                 switchcount++;
  120.             } else {
  121.                 if (switchcount == 0 && dir == "asc") {
  122.                     dir = "desc";
  123.                     switching = true;
  124.                 }
  125.             }
  126.         }
  127.     }
  128. </script>
  129. </body>
  130. </html>
  131.  
Advertisement
Add Comment
Please, Sign In to add comment