I have a data table that displays transaction counts by the hour, from 00 to 23. As such depending on when the report is being viewed number of columns changes. Columns are therefore created dynamically. It works for showing today's data (I see 10 columns from 00 to 09). When I try to view yesterday's data (00 - 23), it shows javascript error in data table's js file.
Below is the code and error message:
<div id="divGrid" style="clear: both">
<table id="txnTable" class="table table-striped table-bordered table-hover display responsive compact" style="width: 100%;">
</table>
</div>
Parameter SelDate is initially today's date and user can change using a calendar control.
function populateTable() {
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
cache: false,
url: '../WebService/ABC.asmx/GetTransactionCountByDay',
data: JSON.stringify({ SelDate: selDate, LogType: -1 }),
}).done(function (result) {debugger
jResult = JSON.parse(result.d);
var columnNames = [];
var tblData = [];
//result.forEach(item => { // IE can't handle this
jResult.forEach(function(item) {
// build an object containing the column names:
columnNames.push({ title: item.HR });
// build an array containing the one row data
tblData.push(item.TXN_COUNT);
});
if ($.fn.DataTable.isDataTable(tblTxn)) {
tblTxn.destroy();
}
tblTxn = $('#txnTable').DataTable({
destroy: true,
data: [tblData],
columns: columnNames
});
}).fail(function (jqXHR, textStatus, errorThrown) {debugger
alert(jqXHR.responseText);
});
}
When selected date is changed and "Submit" button clicked:
$(document).on("click", "#lblSubmit", function (event) {
populateTable();
});
Sample data received from ajax call:
[
{
"DT": "2021-10-20",
"HR": "00",
"TXN_COUNT": 138
},
{
"DT": "2021-10-20",
"HR": "01",
"TXN_COUNT": 235
},
{
"DT": "2021-10-20",
"HR": "02",
"TXN_COUNT": 111
},
{
"DT": "2021-10-20",
"HR": "03",
"TXN_COUNT": 120
},
{
"DT": "2021-10-20",
"HR": "04",
"TXN_COUNT": 120
},
{
"DT": "2021-10-20",
"HR": "05",
"TXN_COUNT": 120
},
{
"DT": "2021-10-19",
"HR": "06",
"TXN_COUNT": 318
},
{
"DT": "2021-10-20",
"HR": "07",
"TXN_COUNT": 505
},
{
"DT": "2021-10-20",
"HR": "08",
"TXN_COUNT": 294
},
{
"DT": "2021-10-20",
"HR": "09",
"TXN_COUNT": 95
}
]
Error:
"Uncaught TypeError: Cannot read properties of undefined (reading 'style')"
A screen shot of error message from Chrome debugger is also attached.
