|
| 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>Searchable book list</title> |
| 7 | + <style> |
| 8 | + table { |
| 9 | + border-collapse: collapse; |
| 10 | + width: 100%; |
| 11 | + } |
| 12 | + th, td { |
| 13 | + border: 1px solid black; |
| 14 | + padding: 8px; |
| 15 | + text-align: left; |
| 16 | + } |
| 17 | + th { |
| 18 | + background-color: #f2f2f2; |
| 19 | + cursor: pointer; |
| 20 | + } |
| 21 | + .search-inputs { |
| 22 | + margin-bottom: 10px; |
| 23 | + } |
| 24 | + .search-inputs input { |
| 25 | + margin-right: 10px; |
| 26 | + margin-bottom: 5px; |
| 27 | + } |
| 28 | + </style> |
| 29 | +</head> |
| 30 | +<body> |
| 31 | + <h1>Here's where I keep a list of books I have read.</h1> |
| 32 | + This list was curated by <a href="index.html">Lexington Whalen</a>, beginning from his first year of PhD to end. As he is me, I hope he keeps going! |
| 33 | + |
| 34 | + I typically use this to organize books I found interesting. Please feel free to do whatever you want with it. |
| 35 | + |
| 36 | + <div class="search-inputs"> |
| 37 | + <input type="text" id="titleSearch" placeholder="Search title..."> |
| 38 | + <input type="text" id="authorSearch" placeholder="Search author..."> |
| 39 | + <input type="text" id="dateCompleteSearch" placeholder="Search date complete..."> |
| 40 | + <input type="text" id="pageCountSearch" placeholder="Search page count..."> |
| 41 | + <input type="text" id="ratingSearch" placeholder="Search rating..."> |
| 42 | + <input type="text" id="descriptionSearch" placeholder="Search description..."> |
| 43 | + <button id="clearSearch">Clear Search</button> |
| 44 | + </div> |
| 45 | + |
| 46 | + <table id="bookTable"> |
| 47 | + <thead> |
| 48 | + <tr> |
| 49 | + <th data-sort="title">Title</th> |
| 50 | + <th data-sort="author">Author</th> |
| 51 | + <th data-sort="date-complete">Date Complete</th> |
| 52 | + <th data-sort="page-count">Page Count</th> |
| 53 | + <th data-sort="rating">Rating</th> |
| 54 | + <th data-sort="description">Description</th> |
| 55 | + </tr> |
| 56 | + </thead> |
| 57 | + <tbody> |
| 58 | + <!-- Table body will be populated by JavaScript --> |
| 59 | + </tbody> |
| 60 | + </table> |
| 61 | + |
| 62 | + <p id="bookCount">So far, we have read 0 books</p> |
| 63 | + |
| 64 | + <script> |
| 65 | + const table = document.getElementById('bookTable'); |
| 66 | + const tbody = table.querySelector('tbody'); |
| 67 | + const clearButton = document.getElementById('clearSearch'); |
| 68 | + const searchInputs = { |
| 69 | + title: document.getElementById('titleSearch'), |
| 70 | + author: document.getElementById('authorSearch'), |
| 71 | + 'date-complete': document.getElementById('dateCompleteSearch'), |
| 72 | + 'page-count': document.getElementById('pageCountSearch'), |
| 73 | + rating: document.getElementById('ratingSearch'), |
| 74 | + description: document.getElementById('descriptionSearch') |
| 75 | + }; |
| 76 | + const bookCountElement = document.getElementById('bookCount'); |
| 77 | + let sortOrder = {}; |
| 78 | + |
| 79 | + // Load books from JSON file |
| 80 | + fetch('books/list.json') |
| 81 | + .then(response => response.json()) |
| 82 | + .then(books => { |
| 83 | + populateTable(books); |
| 84 | + updateBookCount(books.length); |
| 85 | + setupEventListeners(); |
| 86 | + }) |
| 87 | + .catch(error => console.error('Error loading books:', error)); |
| 88 | + |
| 89 | + function populateTable(books) { |
| 90 | + books.forEach(book => { |
| 91 | + const row = document.createElement('tr'); |
| 92 | + for (let key in searchInputs) { |
| 93 | + const cell = document.createElement('td'); |
| 94 | + cell.textContent = book[key]; |
| 95 | + row.appendChild(cell); |
| 96 | + } |
| 97 | + tbody.appendChild(row); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + function updateBookCount(count) { |
| 102 | + bookCountElement.textContent = `So far, we have read ${count} books`; |
| 103 | + } |
| 104 | + |
| 105 | + function setupEventListeners() { |
| 106 | + for (let key in searchInputs) { |
| 107 | + searchInputs[key].addEventListener('keyup', searchTable); |
| 108 | + } |
| 109 | + |
| 110 | + clearButton.addEventListener('click', function() { |
| 111 | + for (let key in searchInputs) { |
| 112 | + searchInputs[key].value = ''; |
| 113 | + } |
| 114 | + searchTable(); |
| 115 | + }); |
| 116 | + |
| 117 | + table.querySelector('thead').addEventListener('click', function(e) { |
| 118 | + const th = e.target.closest('th'); |
| 119 | + if (!th) return; |
| 120 | + const column = th.dataset.sort; |
| 121 | + const dataType = (column === 'page-count') ? 'number' : 'string'; |
| 122 | + sortOrder[column] = sortOrder[column] === 'asc' ? 'desc' : 'asc'; |
| 123 | + sortTable(column, dataType, sortOrder[column]); |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + function searchTable() { |
| 128 | + const rows = tbody.getElementsByTagName('tr'); |
| 129 | + for (let i = 0; i < rows.length; i++) { |
| 130 | + const row = rows[i]; |
| 131 | + const cells = row.getElementsByTagName('td'); |
| 132 | + let foundMatch = true; |
| 133 | + |
| 134 | + for (let key in searchInputs) { |
| 135 | + const cellText = cells[Object.keys(searchInputs).indexOf(key)].textContent.toLowerCase(); |
| 136 | + const searchTerm = searchInputs[key].value.toLowerCase(); |
| 137 | + if (searchTerm && !cellText.includes(searchTerm)) { |
| 138 | + foundMatch = false; |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + row.style.display = foundMatch ? '' : 'none'; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + function sortTable(column, dataType, order) { |
| 148 | + const rows = Array.from(tbody.querySelectorAll('tr')); |
| 149 | + const columnIndex = Array.from(table.querySelector('thead tr').children).findIndex(th => th.dataset.sort === column); |
| 150 | + |
| 151 | + rows.sort((a, b) => { |
| 152 | + let aValue = a.children[columnIndex].textContent; |
| 153 | + let bValue = b.children[columnIndex].textContent; |
| 154 | + |
| 155 | + if (dataType === 'number') { |
| 156 | + return order === 'asc' ? Number(aValue) - Number(bValue) : Number(bValue) - Number(aValue); |
| 157 | + } else if (column === 'date-complete') { |
| 158 | + return order === 'asc' ? new Date(aValue) - new Date(bValue) : new Date(bValue) - new Date(aValue); |
| 159 | + } else if (column === 'rating') { |
| 160 | + return order === 'asc' ? Number(aValue.split('/')[0]) - Number(bValue.split('/')[0]) : Number(bValue.split('/')[0]) - Number(aValue.split('/')[0]); |
| 161 | + } else { |
| 162 | + return order === 'asc' ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue); |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + tbody.append(...rows); |
| 167 | + } |
| 168 | + </script> |
| 169 | +</body> |
| 170 | +</html> |
0 commit comments