// Existing rule datatable creator
var existingRuleTable = $('#existingRulesDataTable')
.on( 'error.dt', function () {
$('#todrexitingrules').hide();
$('#errorModal').modal('show');
$('#errorModal').on('shown.bs.modal', function () {
$('#errorModalCloseButton').focus();
$('#existingRuleError').html(
'<p>There was an issue retrieving the data. Please try again.</p>'
+ '<p>If the error keeps occurring, please get in touch.</p>');
.DataTable({
"ordering": false, // Allows ordering
"searching": false, // Searchbox
"paging": true, // Pagination
"info": false, // Shows 'Showing X of X' information
"pagingType": 'simple_numbers', // Shows Previous, page numbers & next buttons only
"pageLength": 10, // Defaults number of rows to display in table. If changing this value change the show/hide below
"dom": '<"top"f>rt<"bottom"lp><"clear">', // Positions table elements
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]], // Sets up the amount of records to display
"fnDrawCallback": function () {
if ($('#existingRulesDataTable').DataTable().rows().count() < 11) {
$("div[class='bottom']").hide(); // Hides paginator & dropdown if less than 11 records returned
} else {
$("div[class='bottom']").show(); // Shows paginator & dropdown if 11 or more records are returned
'ajax': {
"type": 'GET',
"url": "js/dataTable.json", // TODO > Needs to be changed when actual file resolved
"data": function (data) {
return data;
"dataSrc": function(res){
existingRuleTableCount = res.data.length;
return res.data;
"columns": [ // Display JSON data in table
{ "data": "position" },
{ "data": "startTime" },
{ "data": "endTime" },
{ "data": "selectedDays" },
{ "data": "selectedDates" },
{ "data": "selectedMonths" },
{ "data": "timeRange" },
"data": null,
"render": function (data) {
if (buttonclicked == 'Modify') { // Displays the radio button when 'Mod' clicked
return '<label class="c-radio" style="margin-bottom: 0px">'
+ '<input type="radio" name="existingRuleActionRadioButton" value="option1">'
+ '<span class="fa fa-check"></span>'
+ '</label>';
} else if (buttonclicked == 'Delete') { // Displays the delete button when 'Del' clicked
return '<button name="deleteRuleButton" class="btn btn-danger" id="' + data.position + '">'
+ '<i class="fa fa-trash-o" style="font-size: large"></i>'
+ '</button>';
} else {
return ''; // Needed for the 'Add' button click
"createdRow": function (row, data, dataIndex) {
if (data.startTime == 'Anytime') {
$('td:eq(1)', row).attr('colspan', 2).css('text-align', 'center').html('All day'); // Adds COLSPAN attribute, centers the wording and changes it from 'Anytime'
$('td:eq(2)', row).css('display', 'none'); // Hides cell next to the cell with COLSPAN attribute
if (data.timeRange == '-w') {
$('td:eq(6)', row).html('Working hours'); // Changes text returned by JSON if '-w'
} else if (data.timeRange == '-oo') {
$('td:eq(6)', row).html('Out of office hours'); // Changes text returned by JSON if '-oo'
"destroy": true,
});