JavaScript & CSS Code Explanation
JavaScript & CSS Code Explanation
JavaScript (script.js)
document.addEventListener('DOMContentLoaded', function () {
● This line waits for the HTML document to be completely loaded and parsed before
executing the enclosed code.
2. DOM Elements
javascript
● tableBody: The <tbody> element where the table rows will be dynamically inserted.
● pagination: The container for the pagination buttons.
● searchInput: The input field for searching the table content.
● fetchData(page, search): Fetches data from the data.php script with the
specified page and search query.
● Uses the Fetch API to make an asynchronous request and returns the response in
JSON format.
function renderTable(data) {
tableBody.innerHTML = '';
for (const row of data) {
const tr = document.createElement('tr');
for (const cell in row) {
const td = document.createElement('td');
td.textContent = row[cell];
tr.appendChild(td);
}
tableBody.appendChild(tr);
}
}
● renderTable(data): Clears the current table body content and fills it with rows of
data fetched from the server.
if (currentPage > 1) {
pagination.appendChild(createButton('Previous', currentPage -
1));
}
const maxVisibleButtons = 7;
let startPage = Math.max(1, currentPage -
Math.floor(maxVisibleButtons / 2));
let endPage = Math.min(pageCount, startPage + maxVisibleButtons -
1);
● updateTable(): Fetches data using the current page and search query, then updates
the table and pagination.
● searchTable(): Updates the search query and resets the current page to 1, then calls
updateTable() to refresh the data based on the new search term.
updateTable();
● Calls updateTable() to populate the table with the initial set of data when the page is
first loaded.
The JavaScript code in script.js handles dynamic data fetching, table rendering, pagination,
and search functionality. This explanation will help developers understand each part of the
script, enabling them to make modifications as needed.
CSS (styles.css)
1. Body Styles
css
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
2. Container Styles
css
.container {
max-width: 100%;
overflow-x: auto;
}
css
input[type="text"] {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ddd;
border-radius: 5px;
}
4. Table Styles
css
.table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
css
.table th {
background-color: #f4f4f4;
}
.pagination {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
gap: 5px;
}
.pagination button {
background-color: #FFC523;
color: white;
border: none;
padding: 10px 15px;
margin: 0;
cursor: pointer;
font-size: 14px;
border-radius: 5px;
}
.pagination button:hover {
background-color: #FFC523;
}
● Applies to the <button> elements within the pagination container when hovered over.
● background-color: #FFC523;: Ensures that the background color of the buttons
remains the same when hovered over.
.pagination button:disabled {
background-color: #ccc;
cursor: not-allowed;
}
● Applies to the <button> elements within the pagination container when they are
disabled.
● background-color: #ccc;: Sets the background color of disabled buttons to a light
gray.
● cursor: not-allowed;: Changes the cursor to indicate that the button is not
clickable.
11. Active Pagination Button
.pagination button.active {
background-color: #333;
}
● Applies to the <button> elements within the pagination container when they are active.
● background-color: #333;: Sets the background color of the active button to a dark
gray.
This detailed explanation of the CSS styles and their application to the HTML structure will help
any developer understand the current styles and make changes as needed.