Classic Table - MeggiTools
Run
Toggle Theme
Share Link
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Classic Table</title> <style> body { margin: 0; padding: 0; font-family: 'Arial', sans-serif; background: linear-gradient(to left, #a2c2e0, #f5c8b7); display: flex; justify-content: center; align-items: center; height: 100vh; } .container { width: 85%; background: rgba(255, 255, 255, 0.15); border-radius: 10px; padding: 20px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2); } h1 { color: #fff; text-align: center; margin-bottom: 20px; } .search-container { margin-bottom: 20px; display: flex; justify-content: center; } .search-input { width: 100%; max-width: 450px; padding: 10px; border: none; border-radius: 5px; outline: none; background: rgba(255, 255, 255, 0.25); color: #fff; font-size: 16px; } .search-input::placeholder { color: #ddd; } .search-input:focus { background: rgba(255, 255, 255, 0.35); } table { width: 100%; border-collapse: collapse; color: #fff; } th, td { padding: 15px; text-align: left; border-bottom: 1px solid rgba(255, 255, 255, 0.2); } th { background: rgba(255, 255, 255, 0.2); cursor: pointer; } th.sortable:hover { background: rgba(255, 255, 255, 0.3); } tr:hover { background: rgba(255, 255, 255, 0.1); } .sort-icon { font-size: 12px; color: #fff; margin-left: 5px; } .asc::after { content: '▲'; } .desc::after { content: '▼'; } </style> </head> <body> <div class="container"> <h1>Classic Table with Search and Sort</h1> <div class="search-container"> <input type="text" id="searchInput" class="search-input" placeholder="Search..."> </div> <table id="dataTable"> <thead> <tr> <th class="sortable">Product <span class="sort-icon"></span></th> <th class="sortable">Price <span class="sort-icon"></span></th> <th class="sortable">Stock <span class="sort-icon"></span></th> </tr> </thead> <tbody> <tr> <td>Widget A</td> <td>$10.00</td> <td>50</td> </tr> <tr> <td>Widget B</td> <td>$15.00</td> <td>30</td> </tr> <tr> <td>Widget C</td> <td>$7.00</td> <td>20</td> </tr> <tr> <td>Widget D</td> <td>$22.00</td> <td>10</td> </tr> <tr> <td>Widget E</td> <td>$5.00</td> <td>100</td> </tr> </tbody> </table> </div> <script> document.addEventListener('DOMContentLoaded', function () { const searchInput = document.getElementById('searchInput'); const table = document.getElementById('dataTable'); const headers = table.querySelectorAll('th.sortable'); let sortOrder = {}; searchInput.addEventListener('input', function () { const query = searchInput.value.toLowerCase(); const rows = table.querySelectorAll('tbody tr'); rows.forEach(row => { const cells = row.querySelectorAll('td'); let match = false; cells.forEach(cell => { if (cell.textContent.toLowerCase().includes(query)) { match = true; } }); row.style.display = match ? '' : 'none'; }); }); headers.forEach((header, index) => { header.addEventListener('click', function () { const rowsArray = Array.from(table.querySelectorAll('tbody tr')); const sortDirection = sortOrder[index] = (sortOrder[index] === 'asc') ? 'desc' : 'asc'; rowsArray.sort((a, b) => { const cellA = a.children[index].textContent.trim(); const cellB = b.children[index].textContent.trim(); return sortDirection === 'asc' ? cellA.localeCompare(cellB) : cellB.localeCompare(cellA); }); const tbody = table.querySelector('tbody'); tbody.innerHTML = ''; rowsArray.forEach(row => tbody.appendChild(row)); headers.forEach(th => th.classList.remove('asc', 'desc')); header.classList.add(sortDirection); }); }); }); </script> </body> </html>