make report sortable; include a csv download

master
Joshua Herring 3 weeks ago
parent 4dc3bf2cab
commit 20ff37ec43

@ -37,4 +37,4 @@ rebuild: clean build
cleanhurltest:
-sudo chown -R $(USER):$(USER) data
-rm data/gradingreport.sqlite
-rm data/facultyactivity.sqlite

@ -45,16 +45,20 @@ var dashboardTmpl = template.Must(template.New("dashboard").Parse(`<!DOCTYPE htm
<div class="rvt-m-bottom-lg">
<button class="rvt-button rvt-button--secondary" type="button" id="download-csv">Download CSV Report</button>
</div>
<div class="rvt-m-bottom-md">
<label class="rvt-label" for="table-search">Search</label>
<input class="rvt-input" type="search" id="table-search" placeholder="Filter results...">
</div>
<div style="overflow-x:auto;">
<table class="rvt-table rvt-table--cells" id="report-table">
<thead>
<tr>
<th scope="col">Date Submitted</th>
<th scope="col">Name</th>
<th scope="col" data-col="0" style="cursor:pointer;white-space:nowrap;">Date Submitted <span class="sort-ind">&#8597;</span></th>
<th scope="col" data-col="1" style="cursor:pointer;white-space:nowrap;">Name <span class="sort-ind">&#8597;</span></th>
<th scope="col">Title</th>
<th scope="col">Description</th>
<th scope="col">Hyperlink</th>
<th scope="col">Type</th>
<th scope="col" data-col="5" style="cursor:pointer;white-space:nowrap;">Type <span class="sort-ind">&#8597;</span></th>
</tr>
</thead>
<tbody>
@ -76,52 +80,96 @@ var dashboardTmpl = template.Must(template.New("dashboard").Parse(`<!DOCTYPE htm
{{end}}
</main>
<script>
var dlBtn = document.getElementById('download-csv');
if (dlBtn) {
dlBtn.addEventListener('click', function() {
var now = new Date();
var p = function(n) { return n < 10 ? '0' + n : '' + n; };
var stamp = '' + now.getFullYear() + p(now.getMonth()+1) + p(now.getDate()) + p(now.getHours()) + p(now.getMinutes()) + p(now.getSeconds());
var filename = 'Faculty_Activity_Report-' + stamp + '.csv';
(function() {
var table = document.getElementById('report-table');
var allRows = table ? Array.prototype.slice.call(table.querySelectorAll('tbody tr')) : [];
var sortState = {col: -1, dir: 1};
function esc(val) {
val = val || '';
if (val.indexOf('|') !== -1 || val.indexOf('"') !== -1 || val.indexOf('\n') !== -1) {
return '"' + val.replace(/"/g, '""') + '"';
}
return val;
function getCellText(row, col) {
var cells = row.querySelectorAll('td');
return cells[col] ? cells[col].textContent.trim() : '';
}
function apply() {
var term = (document.getElementById('table-search').value || '').toLowerCase();
var rows = allRows.filter(function(row) {
if (!term) return true;
return Array.prototype.some.call(row.querySelectorAll('td'), function(cell) {
return cell.textContent.toLowerCase().indexOf(term) !== -1;
});
});
if (sortState.col >= 0) {
rows.sort(function(a, b) {
var av = getCellText(a, sortState.col).toLowerCase();
var bv = getCellText(b, sortState.col).toLowerCase();
return av < bv ? -sortState.dir : av > bv ? sortState.dir : 0;
});
}
var tbody = table.querySelector('tbody');
while (tbody.firstChild) tbody.removeChild(tbody.firstChild);
rows.forEach(function(r) { tbody.appendChild(r); });
}
var lines = ['sep=|'];
lines.push(['Date Submitted', 'Name', 'Title', 'Description', 'Hyperlink', 'Type'].map(esc).join('|'));
var searchInput = document.getElementById('table-search');
if (searchInput) searchInput.addEventListener('input', apply);
var rows = document.querySelectorAll('#report-table tbody tr');
rows.forEach(function(row) {
var cells = row.querySelectorAll('td');
var fields = [];
cells.forEach(function(cell, i) {
if (i === 4) {
var a = cell.querySelector('a');
fields.push(esc(a ? a.href : ''));
} else {
fields.push(esc(cell.textContent.trim()));
}
if (table) {
Array.prototype.forEach.call(table.querySelectorAll('th[data-col]'), function(th) {
th.addEventListener('click', function() {
var col = parseInt(th.getAttribute('data-col'), 10);
sortState.dir = sortState.col === col ? sortState.dir * -1 : 1;
sortState.col = col;
Array.prototype.forEach.call(table.querySelectorAll('.sort-ind'), function(ind) { ind.textContent = '⇕'; });
th.querySelector('.sort-ind').textContent = sortState.dir === 1 ? '↑' : '↓';
apply();
});
lines.push(fields.join('|'));
});
}
var csv = lines.join('\r\n');
var blob = new Blob([csv], {type: 'text/csv;charset=utf-8;'});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
}
var dlBtn = document.getElementById('download-csv');
if (dlBtn) {
dlBtn.addEventListener('click', function() {
var now = new Date();
var p = function(n) { return n < 10 ? '0' + n : '' + n; };
var stamp = '' + now.getFullYear() + p(now.getMonth()+1) + p(now.getDate()) + p(now.getHours()) + p(now.getMinutes()) + p(now.getSeconds());
function esc(val) {
val = val || '';
if (val.indexOf('|') !== -1 || val.indexOf('"') !== -1 || val.indexOf('\n') !== -1) {
return '"' + val.replace(/"/g, '""') + '"';
}
return val;
}
var lines = ['sep=|'];
lines.push(['Date Submitted', 'Name', 'Title', 'Description', 'Hyperlink', 'Type'].map(esc).join('|'));
Array.prototype.forEach.call(table.querySelectorAll('tbody tr'), function(row) {
var cells = row.querySelectorAll('td');
var fields = [];
Array.prototype.forEach.call(cells, function(cell, i) {
if (i === 4) {
var a = cell.querySelector('a');
fields.push(esc(a ? a.href : ''));
} else {
fields.push(esc(cell.textContent.trim()));
}
});
lines.push(fields.join('|'));
});
var csv = lines.join('\r\n');
var blob = new Blob([csv], {type: 'text/csv;charset=utf-8;'});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = 'Faculty_Activity_Report-' + stamp + '.csv';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
}
})();
</script>
</body>
</html>`))

Loading…
Cancel
Save