Sure! Below is a complete implementation of a modern, dark-themed admin dashboard using HTML, CSS, and JavaScript. The dashboard includes two main sections: Login Data and Category Info. It features smooth navigation, pagination, and the ability to update certain fields in the Category Info section.
Features:
- Dark Theme: Modern dark UI with minimalistic design.
- Responsive Layout: Works well on various screen sizes.
- Pagination: Navigate through paginated data.
- Edit Functionality: Update specific fields in Category Info.
- Smooth User Experience: Seamless interactions without page reloads.
Technologies Used:
- HTML5: Structure of the dashboard.
- CSS3: Styling with Flexbox and Grid for layout, transitions for smooth effects.
- JavaScript (ES6): Fetch API for backend communication, DOM manipulation for dynamic content.
1. HTML Structure
Create an index.html file with the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Admin Dashboard</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="sidebar">
<h2>Admin Dashboard</h2>
<ul>
<li class="active" data-section="login">Login Data</li>
<li data-section="category">Category Info</li>
</ul>
</div>
<div class="main-content">
<header>
<h1 id="section-title">Login Data</h1>
</header>
<section id="content-section">
<!-- Dynamic Content Goes Here -->
</section>
<footer>
<div id="pagination">
<button id="prev-btn">Previous</button>
<span id="page-info">Page 1</span>
<button id="next-btn">Next</button>
</div>
</footer>
</div>
<script src="app.js"></script>
</body>
</html>
Explanation:
- Sidebar: Contains navigation links to switch between "Login Data" and "Category Info".
- Main Content: Displays the selected section's data and includes pagination controls.
2. CSS Styling
Create a styles.css file with the following content:
/* Reset and Base Styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
display: flex;
height: 100vh;
background-color: #121212;
color: #e0e0e0;
}
/* Sidebar Styles */
.sidebar {
width: 250px;
background-color: #1e1e1e;
padding: 20px;
display: flex;
flex-direction: column;
}
.sidebar h2 {
color: #ffffff;
margin-bottom: 30px;
text-align: center;
}
.sidebar ul {
list-style: none;
flex-grow: 1;
}
.sidebar li {
padding: 15px 20px;
margin-bottom: 10px;
border-radius: 4px;
cursor: pointer;
transition: background 0.3s;
}
.sidebar li:hover,
.sidebar li.active {
background-color: #333333;
}
.sidebar li::before {
content: '';
display: inline-block;
margin-right: 10px;
width: 8px;
height: 8px;
background-color: transparent;
border-radius: 50%;
}
.sidebar li.active::before {
background-color: #ffffff;
}
/* Main Content Styles */
.main-content {
flex-grow: 1;
display: flex;
flex-direction: column;
}
header {
padding: 20px;
background-color: #1e1e1e;
border-bottom: 1px solid #333333;
}
header h1 {
font-size: 1.5em;
}
section {
flex-grow: 1;
padding: 20px;
overflow-y: auto;
}
footer {
padding: 10px 20px;
background-color: #1e1e1e;
border-top: 1px solid #333333;
display: flex;
justify-content: center;
align-items: center;
}
#pagination {
display: flex;
align-items: center;
}
#pagination button {
background-color: #2c2c2c;
border: none;
color: #e0e0e0;
padding: 8px 16px;
margin: 0 10px;
border-radius: 4px;
cursor: pointer;
transition: background 0.3s;
}
#pagination button:hover {
background-color: #444444;
}
#page-info {
font-size: 1em;
}
/* Table Styles */
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
thead {
background-color: #2c2c2c;
}
thead th {
padding: 12px;
text-align: left;
border-bottom: 1px solid #444444;
}
tbody tr:nth-child(even) {
background-color: #1e1e1e;
}
tbody td {
padding: 12px;
border-bottom: 1px solid #333333;
}
tbody td img {
width: 50px;
height: 50px;
object-fit: cover;
border-radius: 4px;
}
/* Button Styles */
button.edit-btn,
button.save-btn,
button.cancel-btn {
background-color: #2c2c2c;
border: none;
color: #e0e0e0;
padding: 6px 12px;
margin-right: 5px;
border-radius: 4px;
cursor: pointer;
transition: background 0.3s;
}
button.edit-btn:hover,
button.save-btn:hover,
button.cancel-btn:hover {
background-color: #444444;
}
input[type="text"],
input[type="checkbox"] {
padding: 6px;
border: 1px solid #444444;
background-color: #2c2c2c;
color: #e0e0e0;
border-radius: 4px;
}
input[type="text"]:disabled {
background-color: #1e1e1e;
}
/* Modal Styles */
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(18, 18, 18, 0.8);
display: flex;
justify-content: center;
align-items: center;
visibility: hidden;
}
.modal.active {
visibility: visible;
}
.modal-content {
background-color: #2c2c2c;
padding: 20px;
border-radius: 8px;
width: 300px;
}
.modal-content h2 {
margin-bottom: 20px;
text-align: center;
}
.modal-content input {
width: 100%;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #444444;
border-radius: 4px;
background-color: #1e1e1e;
color: #e0e0e0;
}
.modal-content button {
width: 100%;
padding: 10px;
background-color: #2c2c2c;
border: none;
color: #e0e0e0;
border-radius: 4px;
cursor: pointer;
transition: background 0.3s;
}
.modal-content button:hover {
background-color: #444444;
}
.error {
color: #ff6b6b;
margin-bottom: 10px;
text-align: center;
}
/* Loader Styles */
.loader {
border: 8px solid #2c2c2c;
border-top: 8px solid #e0e0e0;
border-radius: 50%;
width: 60px;
height: 60px;
animation: spin 1s linear infinite;
margin: 50px auto;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
Explanation:
- Dark Theme Colors: Uses shades of dark gray and black with light text for contrast.
- Responsive Layout: Flexbox is used to arrange sidebar and main content.
- Interactive Elements: Buttons and list items have hover effects for better UX.
- Tables: Styled for readability with alternating row colors.
- Modals: For login forms if needed in the future.
- Loader: A simple spinner for loading states.
3. JavaScript Functionality
Create an app.js file with the following content:
// Configuration
const API_ENDPOINTS = {
login: '/api/login_data', // Replace with your backend endpoints
category: '/api/category_info'
};
const ITEMS_PER_PAGE = 10;
// State Management
let currentSection = 'login';
let currentPage = 1;
let totalPages = 1;
let dataCache = {
login: {},
category: {}
};
// Initialize Dashboard
document.addEventListener('DOMContentLoaded', () => {
setupNavigation();
loadSection(currentSection);
setupPagination();
});
// Setup Sidebar Navigation
function setupNavigation() {
const sidebarItems = document.querySelectorAll('.sidebar li');
sidebarItems.forEach(item => {
item.addEventListener('click', () => {
sidebarItems.forEach(i => i.classList.remove('active'));
item.classList.add('active');
const section = item.getAttribute('data-section');
currentSection = section;
currentPage = 1;
loadSection(section);
});
});
}
// Setup Pagination Controls
function setupPagination() {
document.getElementById('prev-btn').addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
loadSection(currentSection);
}
});
document.getElementById('next-btn').addEventListener('click', () => {
if (currentPage < totalPages) {
currentPage++;
loadSection(currentSection);
}
});
}
// Load a Section (Login or Category)
function loadSection(section) {
const sectionTitle = document.getElementById('section-title');
const contentSection = document.getElementById('content-section');
sectionTitle.textContent = section === 'login' ? 'Login Data' : 'Category Info';
contentSection.innerHTML = '<div class="loader"></div>';
// Fetch Data
fetchData(section, currentPage)
.then(data => {
renderSection(section, data);
updatePagination(data.total);
})
.catch(err => {
contentSection.innerHTML = `<p class="error">Error loading data: ${err.message}</p>`;
});
}
// Fetch Data from Backend
async function fetchData(section, page) {
// Check Cache
if (dataCache[section][page]) {
return dataCache[section][page];
}
const response = await fetch(API_ENDPOINTS[section], {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ page, limit: ITEMS_PER_PAGE })
});
if (!response.ok) {
throw new Error(`Failed to fetch ${section} data`);
}
const result = await response.json();
dataCache[section][page] = result;
return result;
}
// Render Section Data
function renderSection(section, data) {
const contentSection = document.getElementById('content-section');
if (section === 'login') {
contentSection.innerHTML = generateLoginTable(data.items);
} else if (section === 'category') {
contentSection.innerHTML = generateCategoryTable(data.items);
setupEditButtons();
}
}
// Generate Login Data Table
function generateLoginTable(items) {
if (items.length === 0) {
return '<p>No login data available.</p>';
}
let table = `
<table>
<thead>
<tr>
<th>UUID</th>
<th>Create Date</th>
<th>Message</th>
<th>Signature</th>
</tr>
</thead>
<tbody>
`;
items.forEach(item => {
table += `
<tr>
<td>${item.uuid}</td>
<td>${new Date(item.create_date).toLocaleString()}</td>
<td>${item.message}</td>
<td>${item.signature}</td>
</tr>
`;
});
table += '</tbody></table>';
return table;
}
// Generate Category Info Table
function generateCategoryTable(items) {
if (items.length === 0) {
return '<p>No category data available.</p>';
}
let table = `
<table>
<thead>
<tr>
<th>UUID</th>
<th>Category Name</th>
<th>URL Category</th>
<th>Original</th>
<th>Thumbnail</th>
<th>Subcategory</th>
<th>Blocked</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
`;
items.forEach(item => {
table += `
<tr data-uuid="${item.uuid}">
<td>${item.uuid}</td>
<td>${item.category_name}</td>
<td>${item.url_category}</td>
<td><input type="text" value="${item.original || ''}" disabled></td>
<td>${item.thumbnail ? `<img src="${item.thumbnail}" alt="Thumbnail">` : 'N/A'}</td>
<td>
<input type="checkbox" ${item.subcategory ? 'checked' : ''} disabled>
</td>
<td>
<input type="checkbox" ${item.blocked ? 'checked' : ''} disabled>
</td>
<td>
<button class="edit-btn">Edit</button>
<button class="save-btn" style="display:none;">Save</button>
<button class="cancel-btn" style="display:none;">Cancel</button>
</td>
</tr>
`;
});
table += '</tbody></table>';
return table;
}
// Update Pagination Info
function updatePagination(totalItems) {
totalPages = Math.ceil(totalItems / ITEMS_PER_PAGE);
document.getElementById('page-info').textContent = `Page ${currentPage} of ${totalPages}`;
}
// Setup Edit Buttons for Category Info
function setupEditButtons() {
const editButtons = document.querySelectorAll('.edit-btn');
editButtons.forEach(btn => {
btn.addEventListener('click', handleEdit);
});
const saveButtons = document.querySelectorAll('.save-btn');
saveButtons.forEach(btn => {
btn.addEventListener('click', handleSave);
});
const cancelButtons = document.querySelectorAll('.cancel-btn');
cancelButtons.forEach(btn => {
btn.addEventListener('click', handleCancel);
});
}
// Handle Edit Action
function handleEdit(event) {
const row = event.target.closest('tr');
const inputs = row.querySelectorAll('input[type="text"], input[type="checkbox"]');
inputs.forEach(input => input.disabled = false);
row.querySelector('.edit-btn').style.display = 'none';
row.querySelector('.save-btn').style.display = 'inline-block';
row.querySelector('.cancel-btn').style.display = 'inline-block';
}
// Handle Save Action
async function handleSave(event) {
const row = event.target.closest('tr');
const uuid = row.getAttribute('data-uuid');
// Collect Updated Data
const originalInput = row.querySelector('input[type="text"]');
const subcategoryInput = row.querySelector('input[type="checkbox"]:nth-child(1)');
const blockedInput = row.querySelector('input[type="checkbox"]:nth-child(2)');
const updatedData = {
uuid,
original: originalInput.value.trim() || null,
subcategory: subcategoryInput.checked,
blocked: blockedInput.checked
};
try {
// Send Update to Backend
const response = await fetch(API_ENDPOINTS.category, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updatedData)
});
if (!response.ok) {
throw new Error('Failed to update category');
}
// Update Cache
if (dataCache.category[currentPage]) {
const index = dataCache.category[currentPage].items.findIndex(item => item.uuid === uuid);
if (index !== -1) {
dataCache.category[currentPage].items[index] = {
...dataCache.category[currentPage].items[index],
...updatedData
};
}
}
// Re-render Section
loadSection(currentSection);
} catch (err) {
alert(`Error: ${err.message}`);
}
}
// Handle Cancel Action
function handleCancel(event) {
const row = event.target.closest('tr');
const inputs = row.querySelectorAll('input[type="text"], input[type="checkbox"]');
inputs.forEach(input => {
input.disabled = true;
if (input.type === 'text') {
// Reset to original value from cache
const uuid = row.getAttribute('data-uuid');
const currentData = dataCache.category[currentPage].items.find(item => item.uuid === uuid);
input.value = currentData.original || '';
} else if (input.type === 'checkbox') {
const uuid = row.getAttribute('data-uuid');
const currentData = dataCache.category[currentPage].items.find(item => item.uuid === uuid);
if (input.parentElement.cellIndex === 5) { // Subcategory
input.checked = currentData.subcategory;
} else if (input.parentElement.cellIndex === 6) { // Blocked
input.checked = currentData.blocked;
}
}
});
row.querySelector('.edit-btn').style.display = 'inline-block';
row.querySelector('.save-btn').style.display = 'none';
row.querySelector('.cancel-btn').style.display = 'none';
}
Explanation:
- API Endpoints: Replace
/api/login_data and /api/category_info with your actual backend endpoints.
- State Management: Keeps track of the current section, page, and caches fetched data to minimize redundant requests.
- Event Listeners: Sets up navigation and pagination controls.
- Data Fetching: Uses
fetch to make POST requests with page and limit parameters for pagination.
- Rendering Tables: Dynamically creates HTML tables based on fetched data.
- Edit Functionality: Allows editing specific fields in the Category Info table and saving changes back to the backend.
- Error Handling: Displays errors if data fetching or updating fails.
4. Backend Integration
Since this is a frontend implementation, you'll need to ensure that your backend APIs are set up to handle the following:
1. Login Data Endpoint (/api/login_data)
- Method: POST
- Request Body:
{
"page": 1,
"limit": 10
}
- Response:
{
"total": 100, // Total number of login records
"items": [
{
"uuid": "123e4567-e89b-12d3-a456-426614174000",
"create_date": "2023-10-05T14:48:00.000Z",
"message": "User logged in",
"signature": "abcdef123456"
},
// More records...
]
}
2. Category Info Endpoint (/api/category_info)
- GET (for fetching data):
- Method: POST
- Request Body:
{
"page": 1,
"limit": 10
}
- Response:
{
"total": 50, // Total number of categories
"items": [
{
"uuid": "cat-001",
"category_name": "Electronics",
"url_category": "/electronics",
"original": null,
"thumbnail": "https://example.com/thumb_electronics.jpg",
"subcategory": false,
"blocked": false
},
// More records...
]
}
- POST (for updating data):
- Method: POST
- Request Body:
{
"uuid": "cat-001",
"original": "Original Category Name", // or null
"subcategory": true,
"blocked": false
}
- Response:
{
"success": true,
"message": "Category updated successfully."
}
Note: Ensure your backend supports CORS if the frontend and backend are on different domains.
5. Final Touches
A. Handling No Data Scenarios
The JavaScript code includes checks to display messages when there's no data available for a section.
B. Loader Indicator
A spinner is displayed while data is being fetched to indicate loading states.
C. Accessibility Considerations
- Ensure all interactive elements are keyboard accessible.
- Use semantic HTML where possible.
D. Optimizations
- Implement debounce on pagination or search if added in the future.
- Enhance caching strategies based on application needs.
6. Deployment
To deploy this dashboard:
-
File Structure:
/your-project
│
├── index.html
├── styles.css
└── app.js
-
Serve the Files: You can use any static server. For development purposes, you can use Live Server if you're using VS Code.
-
Backend Integration: Ensure your backend APIs are up and running and accessible to the frontend.
Conclusion
This implementation provides a solid foundation for an admin dashboard with a modern dark theme. It includes essential features like data display, pagination, and editable fields, all crafted to ensure a smooth and seamless user experience. You can further enhance this dashboard by adding features like search, filters, real-time updates, and more based on your requirements.
Feel free to customize the styles and functionalities to better fit your specific needs!