update
This commit is contained in:
parent
7915228119
commit
e84b97fe40
@ -103,7 +103,6 @@ app.get('/home', isAuthenticated, (req, res) => {
|
|||||||
res.render('home', { username: req.session.username, loginLogs: loginResults });
|
res.render('home', { username: req.session.username, loginLogs: loginResults });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/inusers', isAuthenticated, (req, res) => {
|
app.get('/inusers', isAuthenticated, (req, res) => {
|
||||||
// Fetch all user data from the database
|
// Fetch all user data from the database
|
||||||
const allUsersQuery = 'SELECT * FROM users';
|
const allUsersQuery = 'SELECT * FROM users';
|
||||||
@ -115,11 +114,15 @@ app.get('/inusers', isAuthenticated, (req, res) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render the inusers page with all user data
|
// Render the inusers page with JSON data
|
||||||
res.render('inusers', { allUsers: allUsers });
|
res.render('inusers', { allUsers });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app.use(express.static('views'));
|
app.use(express.static('views'));
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
|
||||||
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
|
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.4/xlsx.full.min.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.4/xlsx.full.min.js"></script>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.5/xlsx.full.min.js"></script>
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
@ -156,93 +157,91 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<!-- Data will be populated dynamically using JavaScript -->
|
<% if (allUsers && allUsers.length > 0) { %>
|
||||||
|
<% allUsers.forEach(user => { %>
|
||||||
|
<tr>
|
||||||
|
<td><%= user.name %></td>
|
||||||
|
<td><%= user.username %></td>
|
||||||
|
<td><%= user.email %></td>
|
||||||
|
<td><%= user.password %></td>
|
||||||
|
<td><%= new Date(user.lastLogin).toLocaleString('en-US', { timeZone: 'Asia/Singapore' }) %></td>
|
||||||
|
<td><%= user.jobTitle %></td>
|
||||||
|
<!-- Add more table data cells as needed -->
|
||||||
|
</tr>
|
||||||
|
<% }); %>
|
||||||
|
<% } else { %>
|
||||||
|
<tr>
|
||||||
|
<td colspan="6">No users available.</td>
|
||||||
|
</tr>
|
||||||
|
<% } %>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Additional content for In-House Users page goes here -->
|
<!-- Additional content for In-House Users page goes here -->
|
||||||
|
<!-- Additional content for In-House Users page goes here -->
|
||||||
|
|
||||||
<script>
|
<!-- Additional content for In-House Users page goes here -->
|
||||||
document.getElementById('sidebarCollapse').addEventListener('click', function () {
|
|
||||||
document.getElementById('sidebar').style.width = (document.getElementById('sidebar').style.width === '250px') ? '0' : '250px';
|
<!-- Additional content for In-House Users page goes here -->
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// Use the JSON data provided by the server
|
||||||
|
const allUsers = <%- JSON.stringify(allUsers) %>;
|
||||||
|
|
||||||
|
|
||||||
|
// Attach click event to the download button
|
||||||
|
document.getElementById('downloadButton').addEventListener('click', function () {
|
||||||
|
console.log('Download button clicked');
|
||||||
|
downloadExcel(allUsers);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Function to download data as Excel
|
// Function to download data as Excel
|
||||||
function downloadExcel() {
|
function downloadExcel(allUsers) {
|
||||||
// Assuming 'allUsers' is defined in the global scope
|
if (allUsers && allUsers.length > 0) {
|
||||||
if (window.allUsers && window.allUsers.length > 0) {
|
const data = allUsers.map(user => [
|
||||||
const data = window.allUsers.map(user => [
|
|
||||||
user.name,
|
user.name,
|
||||||
user.username,
|
user.username,
|
||||||
user.email,
|
user.email,
|
||||||
user.password,
|
user.password,
|
||||||
new Date(user.lastLogin).toLocaleString('en-US', { timeZone: 'Asia/Singapore' }),
|
new Date(user.lastLogin).toLocaleString('en-US', { timeZone: 'Asia/Singapore' }),
|
||||||
user.jobTitle
|
user.jobTitle
|
||||||
// Add more data fields as needed
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const ws = XLSX.utils.aoa_to_sheet([['Name', 'Username', 'Email', 'Password', 'Last Login', 'Job Title'], ...data]);
|
const ws = XLSX.utils.aoa_to_sheet([['Name', 'Username', 'Email', 'Password', 'Last Login', 'Job Title'], ...data]);
|
||||||
const wb = XLSX.utils.book_new();
|
const wb = XLSX.utils.book_new();
|
||||||
XLSX.utils.book_append_sheet(wb, ws, 'All Users');
|
XLSX.utils.book_append_sheet(wb, ws, 'All Users');
|
||||||
XLSX.writeFile(wb, 'user_data.xlsx');
|
|
||||||
|
// Use the correct MIME type for the blob
|
||||||
|
const blob = XLSX.write(wb, { bookType: 'xlsx', mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
|
||||||
|
|
||||||
|
// Create a Blob containing the Excel file data
|
||||||
|
const blobData = new Blob([blob], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
|
||||||
|
|
||||||
|
// Create a Blob URL
|
||||||
|
const blobUrl = URL.createObjectURL(blobData);
|
||||||
|
|
||||||
|
// Create an anchor element and trigger the download
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.href = blobUrl;
|
||||||
|
a.download = 'user_data.xlsx';
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.click();
|
||||||
|
|
||||||
|
// Clean up resources
|
||||||
|
document.body.removeChild(a);
|
||||||
|
URL.revokeObjectURL(blobUrl);
|
||||||
} else {
|
} else {
|
||||||
console.error('No data available for download.');
|
console.error('No data available for download.');
|
||||||
// Handle the case where there is no data
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
// Function to display user data
|
|
||||||
function displayUserData() {
|
|
||||||
$.ajax({
|
|
||||||
url: '/inusers/userdata',
|
|
||||||
method: 'GET',
|
|
||||||
dataType: 'json', // Assuming the response is JSON
|
|
||||||
success: function (data) {
|
|
||||||
window.allUsers = data; // Assuming the response contains user data
|
|
||||||
populateTable(data);
|
|
||||||
},
|
|
||||||
error: function (error) {
|
|
||||||
console.error('Error fetching user data:', error);
|
|
||||||
// Handle error as needed
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to populate the table with user data
|
|
||||||
function populateTable(users) {
|
|
||||||
const tbody = document.querySelector('tbody');
|
|
||||||
tbody.innerHTML = ''; // Clear existing data
|
|
||||||
|
|
||||||
if (users && users.length > 0) {
|
|
||||||
users.forEach(user => {
|
|
||||||
const row = document.createElement('tr');
|
|
||||||
row.innerHTML = `
|
|
||||||
<td>${user.name}</td>
|
|
||||||
<td>${user.username}</td>
|
|
||||||
<td>${user.email}</td>
|
|
||||||
<td>${user.password}</td>
|
|
||||||
<td>${new Date(user.lastLogin).toLocaleString('en-US', { timeZone: 'Asia/Singapore' })}</td>
|
|
||||||
<td>${user.jobTitle}</td>
|
|
||||||
<!-- Add more table data cells as needed -->
|
|
||||||
`;
|
|
||||||
tbody.appendChild(row);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
const row = document.createElement('tr');
|
|
||||||
row.innerHTML = `<td colspan="6">No users available.</td>`;
|
|
||||||
tbody.appendChild(row);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call the function when the page loads
|
|
||||||
displayUserData();
|
|
||||||
|
|
||||||
// Attach click event to the download button
|
|
||||||
document.getElementById('downloadButton').addEventListener('click', function () {
|
|
||||||
downloadExcel();
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user