Refactor table display
This commit is contained in:
parent
78c9ec522d
commit
2a2f8011b9
2 changed files with 51 additions and 74 deletions
|
@ -43,6 +43,17 @@
|
||||||
<h3>Your Report History</h3>
|
<h3>Your Report History</h3>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
|
<table class="table table-striped table-responsive-sm" style="visibility:hidden">
|
||||||
|
<thead>
|
||||||
|
<th>Title</th>
|
||||||
|
<th>Date Created</th>
|
||||||
|
<th class="d-none d-lg-table-cell">State</th>
|
||||||
|
<th class="d-none d-md-table-cell">Date Submitted</th>
|
||||||
|
<th>Action</th>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -223,12 +223,18 @@ function createEditReportForm(parsedData) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayListOfReports(parsedData) {
|
function displayListOfReports(parsedData) {
|
||||||
const cardBody = document.querySelector(".card-body");
|
|
||||||
const table = document.createElement("table");
|
|
||||||
const reports = parsedData.reports;
|
const reports = parsedData.reports;
|
||||||
let reportsAdded = 0;
|
|
||||||
|
|
||||||
// Create report table
|
if (reports.length === 0) {
|
||||||
|
const cardBody = document.querySelector(".card-body");
|
||||||
|
const p = document.createElement("p");
|
||||||
|
p.innerHTML = "No reports found.";
|
||||||
|
cardBody.appendChild(p);
|
||||||
|
} else {
|
||||||
|
const table = document.querySelector("table");
|
||||||
|
const tbody = document.querySelector("tbody");
|
||||||
|
|
||||||
|
// Insert data into the table row
|
||||||
for (let i = 0; i < reports.length; i++) {
|
for (let i = 0; i < reports.length; i++) {
|
||||||
let title = reports[i].title;
|
let title = reports[i].title;
|
||||||
let dateCreated = new Date(reports[i].date_created).toLocaleDateString("en-US");
|
let dateCreated = new Date(reports[i].date_created).toLocaleDateString("en-US");
|
||||||
|
@ -236,6 +242,14 @@ function displayListOfReports(parsedData) {
|
||||||
let dateSubmitted;
|
let dateSubmitted;
|
||||||
let rid = reports[i].report_pk;
|
let rid = reports[i].report_pk;
|
||||||
|
|
||||||
|
let bodyRow = tbody.insertRow(i);
|
||||||
|
bodyRow.insertCell(0).innerHTML = title;
|
||||||
|
bodyRow.insertCell(1).innerHTML = dateCreated;
|
||||||
|
|
||||||
|
let stateCell = bodyRow.insertCell(2);
|
||||||
|
stateCell.innerHTML = state;
|
||||||
|
stateCell.classList.add("d-none", "d-lg-table-cell"); // Column visible only on large displays
|
||||||
|
|
||||||
// Create edit/view button
|
// Create edit/view button
|
||||||
let actionButton = document.createElement("button");
|
let actionButton = document.createElement("button");
|
||||||
actionButton.type = "submit";
|
actionButton.type = "submit";
|
||||||
|
@ -255,61 +269,13 @@ function displayListOfReports(parsedData) {
|
||||||
actionButton.innerHTML = "View";
|
actionButton.innerHTML = "View";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert data into the table object
|
|
||||||
let bodyRow = table.insertRow(i);
|
|
||||||
bodyRow.insertCell(0).innerHTML = title;
|
|
||||||
bodyRow.insertCell(1).innerHTML = dateCreated;
|
|
||||||
|
|
||||||
let stateCell = bodyRow.insertCell(2);
|
|
||||||
stateCell.innerHTML = state;
|
|
||||||
stateCell.classList.add("d-none", "d-lg-table-cell"); // Column visible only on large displays
|
|
||||||
|
|
||||||
let dateSubmittedCell = bodyRow.insertCell(3);
|
let dateSubmittedCell = bodyRow.insertCell(3);
|
||||||
dateSubmittedCell.innerHTML = dateSubmitted;
|
dateSubmittedCell.innerHTML = dateSubmitted;
|
||||||
dateSubmittedCell.classList.add("d-none", "d-md-table-cell"); // Column visible on medium and larger displays
|
dateSubmittedCell.classList.add("d-none", "d-md-table-cell"); // Column visible on medium and larger displays
|
||||||
|
|
||||||
bodyRow.insertCell(4).appendChild(actionButton);
|
bodyRow.insertCell(4).appendChild(actionButton);
|
||||||
reportsAdded++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (reportsAdded === 0) {
|
table.style.visibility = "visible";
|
||||||
// Report list is empty
|
|
||||||
const p = document.createElement("p");
|
|
||||||
p.innerHTML = "No reports found.";
|
|
||||||
cardBody.appendChild(p);
|
|
||||||
} else {
|
|
||||||
// Report list exists and table rows have been created
|
|
||||||
// Create table header, add it to the table, and append the result to the card body
|
|
||||||
|
|
||||||
const tr = document.createElement("tr");
|
|
||||||
|
|
||||||
const headTitle = document.createElement("th");
|
|
||||||
headTitle.innerHTML = "Title";
|
|
||||||
tr.appendChild(headTitle);
|
|
||||||
|
|
||||||
const headDateCreated = document.createElement("th");
|
|
||||||
headDateCreated.innerHTML = "Date Created";
|
|
||||||
tr.appendChild(headDateCreated);
|
|
||||||
|
|
||||||
const headState = document.createElement("th");
|
|
||||||
headState.innerHTML = "State";
|
|
||||||
headState.classList.add("d-none", "d-lg-table-cell"); // Column visible only on large displays
|
|
||||||
tr.appendChild(headState);
|
|
||||||
|
|
||||||
const headDateSubmitted = document.createElement("th")
|
|
||||||
headDateSubmitted.innerHTML = "Date Submitted";
|
|
||||||
headDateSubmitted.classList.add("d-none", "d-md-table-cell"); // Column visible on medium and larger displays
|
|
||||||
tr.appendChild(headDateSubmitted);
|
|
||||||
|
|
||||||
const headAction = document.createElement("th")
|
|
||||||
headAction.innerHTML = "Action";
|
|
||||||
tr.appendChild(headAction);
|
|
||||||
|
|
||||||
const thead = document.createElement("thead");
|
|
||||||
thead.appendChild(tr);
|
|
||||||
table.prepend(thead);
|
|
||||||
table.classList.add("table", "table-striped", "table-responsive-sm");
|
|
||||||
cardBody.appendChild(table);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue