Skip to content

Instantly share code, notes, and snippets.

@dperussina
Created September 27, 2017 15:54
Show Gist options
  • Save dperussina/596807f9e1b5217b281d74f83469da5f to your computer and use it in GitHub Desktop.
Save dperussina/596807f9e1b5217b281d74f83469da5f to your computer and use it in GitHub Desktop.
var client = require('./http');
var qs = require('querystring');
var u = require('./u/u')
function main() {
var dateInput = $("#date-input");
var loadIdInput = $("#load-id-input");
var searchBtn = $("#search-btn");
var exportBtn = $("#export-btn");
var status = $("#status-fill")
exportBtn.click(function (e) {
e.preventDefault();
var table = $("table").html();
table = table.replace('<thead>', '');
table = table.replace('</thead>', '');
table = table.replace('<tbody>', '');
table = table.replace('</tbody>', '');
window.open('data:application/vnd.ms-excel,' + "<table>" + table + "</table>");
});
searchBtn.click(function (e) {
e.preventDefault();
status.text('Searching...');
if (loadIdInput.val() == "") {
status.text('Error! Invalid input values...');
return;
}
var query = qs.stringify({ id: loadIdInput.val() });
u.log('[query]', query)
client.get('/api/Get_LoadIDReport?' + query, function (err, res) {
u.log('/api/Get_LoadIDReport?' + query, err, res);
if (err) {
status.text('Error! Something went wrong...' + res.toString());
return;
}
status.text('Success! Loading ' + res.data.length + ' results..');
processResults(res.data);
//u.log('/api/GetOnHandByDateRange?'+query, err, res);
});
});
function processResults(data) {
var html = "";
var header = "";
u.log('[processResults] ', data.length);
data.forEach(function (e, i, a) {
if (i == 0) {
header += '<tr>';
for (x in e) {
header += '<th>';
header += x;
header += '</th>';
}
header += '</tr>';
$("#head-fill").html(header);
}
html += '<tr>';
for (x in e) {
html += '<td>';
if (e[x] != null && e[x].indexOf('-') > -1 && e[x].indexOf('T') > -1) {
try {
html += formatDate(e[x]);
} catch (err) {
html += e[x] == null ? 'Not Found' : e[x];
}
} else {
html += e[x] == null ? 'Not Found' : e[x];
}
html += '</td>';
}
html += '</tr>';
});
$("#results-fill").html(html);
status.text('Done! Order data processed successfully!')
}
}
function formatDate(date) {
try {
var _date = date.split('T')[0]
return _date;
} catch (err) {
return date;
}
}
$(function () {
main();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment