<!
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Table CRUD with Sorting, Pagination, Search, and Export</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th, td {
border: 1px solid #ccc;
padding: 10px;
text-align: left;
}
th {
cursor: pointer;
background-color: #f4f4f4;
}
.pagination {
display: flex;
justify-content: center;
gap: 5px;
}
.pagination button {
padding: 5px 10px;
cursor: pointer;
}
.hidden {
display: none;
}
.context-menu {
position: absolute;
display: none;
background-color: white;
border: 1px solid #ccc;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
z-index: 1000;
}
.context-menu ul {
list-style: none;
margin: 0;
padding: 0;
}
.context-menu li {
padding: 8px 12px;
cursor: pointer;
}
.context-menu li:hover {
background-color: #f4f4f4;
}
</style>
</head>
<body>
<h1>Table with CRUD, Sorting, Pagination, Context Menu, Search, and Export</h1>
<input type="text" id="search" placeholder="Search...">
<button onclick="exportToExcel()">Export to Excel</button>
<table id="dataTable">
<thead>
<tr>
<th onclick="sortTable(0)">ID</th>
<th onclick="sortTable(1)">Name</th>
<th onclick="sortTable(2)">Age</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<!-- Dynamic rows will be added here -->
</tbody>
</table>
<div class="pagination" id="pagination"></div>
<div class="context-menu" id="contextMenu">
<ul>
<li onclick="editRow()">Edit</li>
<li onclick="deleteRow()">Delete</li>
</ul>
</div>
<script>
const tableData = [
{ id: 1, name: "John Doe", age: 25 },
{ id: 2, name: "Jane Smith", age: 30 },
{ id: 3, name: "Alice Johnson", age: 28 },
{ id: 4, name: "Bob Brown", age: 35 },
{ id: 5, name: "Charlie Davis", age: 22 }
];
let currentPage = 1;
const rowsPerPage = 3;
let currentContextRow = null;
const tableBody = [Link]("#dataTable tbody");
const pagination = [Link]("pagination");
const contextMenu = [Link]("contextMenu");
function renderTable() {
[Link] = "";
const start = (currentPage - 1) * rowsPerPage;
const end = start + rowsPerPage;
const filteredData = [Link](start, end);
[Link](row => {
const tr = [Link]("tr");
[Link] = `
<td>${[Link]}</td>
<td>${[Link]}</td>
<td>${[Link]}</td>
<td>
<button onclick="editRow(${[Link]})">Edit</button>
<button onclick="deleteRow(${[Link]})">Delete</button>
</td>
`;
[Link]("contextmenu", (e) => {
[Link]();
currentContextRow = [Link];
showContextMenu([Link], [Link]);
});
[Link](tr);
});
renderPagination();
}
function renderPagination() {
[Link] = "";
const totalPages = [Link]([Link] / rowsPerPage);
for (let i = 1; i <= totalPages; i++) {
const button = [Link]("button");
[Link] = i;
[Link] = i === currentPage ? "active" : "";
[Link] = () => {
currentPage = i;
renderTable();
};
[Link](button);
}
}
function sortTable(columnIndex) {
[Link]((a, b) => {
const valA = [Link](a)[columnIndex];
const valB = [Link](b)[columnIndex];
return valA > valB ? 1 : -1;
});
renderTable();
}
function searchTable() {
const searchValue =
[Link]("search").[Link]();
[Link] = "";
[Link](row =>
[Link](row).some(value =>
[Link]().toLowerCase().includes(searchValue)
)
).forEach(row => {
const tr = [Link]("tr");
[Link] = `
<td>${[Link]}</td>
<td>${[Link]}</td>
<td>${[Link]}</td>
<td>
<button onclick="editRow(${[Link]})">Edit</button>
<button onclick="deleteRow(${[Link]})">Delete</button>
</td>
`;
[Link](tr);
});
}
function editRow(id) {
const row = [Link](r => [Link] === id);
const newName = prompt("Enter new name:", [Link]);
const newAge = prompt("Enter new age:", [Link]);
if (newName && newAge) {
[Link] = newName;
[Link] = parseInt(newAge, 10);
renderTable();
}
}
function deleteRow(id) {
const index = [Link](r => [Link] === id);
if (index !== -1) {
[Link](index, 1);
renderTable();
}
}
function exportToExcel() {
const csv = ["ID,Name,Age"];
[Link](row => {
[Link](`${[Link]},${[Link]},${[Link]}`);
});
const blob = new Blob([[Link]("\n")], { type: "text/csv" });
const url = [Link](blob);
const a = [Link]("a");
[Link] = url;
[Link] = "table_data.csv";
[Link]();
[Link](url);
}
function showContextMenu(x, y) {
[Link] = `${x}px`;
[Link] = `${y}px`;
[Link] = "block";
}
function hideContextMenu() {
[Link] = "none";
}
[Link]("click", hideContextMenu);
[Link]("search").addEventListener("input", searchTable);
renderTable();
</script>
</body>
</html>