|
1 | 1 | function setupSearch() { |
| 2 | + // Pagination configuration |
| 3 | + var RESULTS_PER_PAGE = 25; // between 20-30 as requested |
| 4 | + var currentResultIndices = []; // indices of rows matching current search |
| 5 | + var currentPage = 1; |
| 6 | + var totalPages = 1; |
| 7 | + var paginationContainer = null; |
2 | 8 | var table = document.querySelector('table#companies-table'); |
3 | 9 |
|
4 | 10 | // ✅ Search Input Field |
@@ -93,18 +99,132 @@ function setupSearch() { |
93 | 99 | searchMatches[+r.ref] = r; |
94 | 100 | }); |
95 | 101 |
|
96 | | - // ✅ 5️⃣ Show/Hide Table Rows Based on Match |
97 | | - searchData.textData.forEach(function (company, index) { |
98 | | - var match = searchMatches[index]; |
| 102 | + // Build list of indices that match (or all if allMatch) |
| 103 | + currentResultIndices = []; |
| 104 | + searchData.textData.forEach(function(company, index) { |
| 105 | + if (allMatch || searchMatches[index]) { |
| 106 | + currentResultIndices.push(index); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + // If showing all (no search) we do NOT paginate to avoid changing existing UX |
| 111 | + if (allMatch) { |
| 112 | + removePagination(); |
| 113 | + showRowsUnpaginated(currentResultIndices); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + setupOrUpdatePagination(); |
| 118 | + renderPage(1); // reset to first page on new / updated search |
| 119 | + } |
| 120 | + |
| 121 | + function showRowsUnpaginated(indices) { |
| 122 | + // Show all listed indices, hide others |
| 123 | + searchData.textData.forEach(function(company, index) { |
99 | 124 | var row = document.getElementById('company-row-' + index); |
100 | 125 | if (!row) return; |
101 | | - var rowMatch = row.nextElementSibling; |
102 | | - if (rowMatch && rowMatch.classList.contains('company-match')) { |
103 | | - rowMatch.parentNode.removeChild(rowMatch); |
| 126 | + row.style.display = indices.indexOf(index) !== -1 ? '' : 'none'; |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + function setupOrUpdatePagination() { |
| 131 | + if (!paginationContainer) { |
| 132 | + paginationContainer = document.createElement('div'); |
| 133 | + paginationContainer.id = 'pagination'; |
| 134 | + paginationContainer.style.margin = '1em 0'; |
| 135 | + paginationContainer.style.display = 'flex'; |
| 136 | + paginationContainer.style.flexWrap = 'wrap'; |
| 137 | + paginationContainer.style.gap = '4px'; |
| 138 | + // Insert after the search explanation (which is before the table) |
| 139 | + var searchExplanation = document.getElementById('search-explanation'); |
| 140 | + searchExplanation.parentNode.insertBefore(paginationContainer, searchExplanation.nextSibling); |
| 141 | + } |
| 142 | + // Compute total pages |
| 143 | + totalPages = Math.max(1, Math.ceil(currentResultIndices.length / RESULTS_PER_PAGE)); |
| 144 | + buildPaginationControls(); |
| 145 | + } |
| 146 | + |
| 147 | + function removePagination() { |
| 148 | + currentPage = 1; |
| 149 | + totalPages = 1; |
| 150 | + if (paginationContainer) { |
| 151 | + paginationContainer.innerHTML = ''; |
| 152 | + paginationContainer.style.display = 'none'; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + function buildPaginationControls() { |
| 157 | + if (!paginationContainer) return; |
| 158 | + paginationContainer.style.display = totalPages > 1 ? 'flex' : 'none'; |
| 159 | + paginationContainer.innerHTML = ''; |
| 160 | + |
| 161 | + function makeButton(label, disabled, onClick, isActive) { |
| 162 | + var btn = document.createElement('button'); |
| 163 | + btn.textContent = label; |
| 164 | + btn.style.padding = '4px 8px'; |
| 165 | + btn.style.border = '1px solid #ccc'; |
| 166 | + btn.style.background = isActive ? '#0366d6' : '#f6f8fa'; |
| 167 | + btn.style.color = isActive ? '#fff' : '#000'; |
| 168 | + btn.style.cursor = disabled ? 'not-allowed' : 'pointer'; |
| 169 | + btn.disabled = !!disabled; |
| 170 | + btn.addEventListener('click', function(e) { |
| 171 | + e.preventDefault(); |
| 172 | + if (!disabled) onClick(); |
| 173 | + }); |
| 174 | + return btn; |
| 175 | + } |
| 176 | + |
| 177 | + // Previous button |
| 178 | + paginationContainer.appendChild(makeButton('Prev', currentPage === 1, function() { |
| 179 | + renderPage(currentPage - 1); |
| 180 | + })); |
| 181 | + |
| 182 | + // Decide page number buttons to show (simple strategy: show all if <= 10, else window around current) |
| 183 | + var pagesToShow = []; |
| 184 | + if (totalPages <= 10) { |
| 185 | + for (var i = 1; i <= totalPages; i++) pagesToShow.push(i); |
| 186 | + } else { |
| 187 | + var windowSize = 3; // pages on each side |
| 188 | + var start = Math.max(1, currentPage - windowSize); |
| 189 | + var end = Math.min(totalPages, currentPage + windowSize); |
| 190 | + if (start > 1) pagesToShow.push(1, '...'); |
| 191 | + for (var p = start; p <= end; p++) pagesToShow.push(p); |
| 192 | + if (end < totalPages) pagesToShow.push('...', totalPages); |
| 193 | + } |
| 194 | + |
| 195 | + pagesToShow.forEach(function(p) { |
| 196 | + if (p === '...') { |
| 197 | + var span = document.createElement('span'); |
| 198 | + span.textContent = '...'; |
| 199 | + span.style.padding = '4px 8px'; |
| 200 | + paginationContainer.appendChild(span); |
| 201 | + } else { |
| 202 | + paginationContainer.appendChild(makeButton(String(p), false, function() { renderPage(p); }, p === currentPage)); |
104 | 203 | } |
105 | | - row.style.display = (match || allMatch) ? '' : 'none'; |
106 | | - row.classList.remove('has-match'); |
107 | 204 | }); |
| 205 | + |
| 206 | + // Next button |
| 207 | + paginationContainer.appendChild(makeButton('Next', currentPage === totalPages, function() { |
| 208 | + renderPage(currentPage + 1); |
| 209 | + })); |
| 210 | + } |
| 211 | + |
| 212 | + function renderPage(page) { |
| 213 | + page = Math.min(Math.max(1, page), totalPages); |
| 214 | + currentPage = page; |
| 215 | + var start = (page - 1) * RESULTS_PER_PAGE; |
| 216 | + var end = start + RESULTS_PER_PAGE; |
| 217 | + var pageSet = {}; |
| 218 | + currentResultIndices.slice(start, end).forEach(function(idx) { pageSet[idx] = true; }); |
| 219 | + |
| 220 | + // Update rows |
| 221 | + searchData.textData.forEach(function(company, index) { |
| 222 | + var row = document.getElementById('company-row-' + index); |
| 223 | + if (!row) return; |
| 224 | + row.style.display = pageSet[index] ? '' : 'none'; |
| 225 | + }); |
| 226 | + |
| 227 | + buildPaginationControls(); // refresh controls to reflect current page |
108 | 228 | } |
109 | 229 |
|
110 | 230 | // ✅ Search Data Loading on Focus |
|
0 commit comments