Key Points of Your Requirement:
Date Sorting: Actual dates should be sorted normally.
Empty Cells: Date Empty cells or data-sort=“01-Jan-3000” should always be at the bottom of the list.
Behavior Consistency: This behavior should be consistent across both ascending and descending sorts.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DataTable Example</title>
<!-- Include jQuery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- Include DataTables CSS and JS -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css">
<script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>
</head>
<body>
<table id="meds-condition" tabindex="-1"
class="table--base js-datatable js-table--sorting js-datatable--pagination dataTable no-footer"
data-page-length="25" data-sZeroRecords='No information available'
data-sort-order='[
[[0, "asc"],[3, "desc"]],
[[1, "asc"],[3, "desc"],[0, "asc"]],
[[3, "asc"],[0, "asc"]],
[[3, "asc"],[0, "asc"]]
]'>
<thead>
<tr>
<th class="cell-width-45p">Name</th>
<th class="cell-width-15p table__colnosort">Date of Onset</th>
<th class="cell-width-25p table__colnosort">Source Author</th>
<th class="cell-width-15p">Document Date</th>
</tr>
</thead>
<tbody>
<!-- Example rows, replace these with your actual data -->
<tr data-href="javascript:void(0);" data-doctype="100.16645" data-sly-test.repoIdHypenDocId="2-2" data-sly-test.repoIdSlashDocId="2/2">
<td data-th="Name" class="cell-width-16p">Condition 2</td>
<td data-th="Date of Onset" data-sort="13-Feb-2024" class="cell-width-16p">13-Feb-2024</td>
<td data-th="Source Author" class="cell-width-16p">Author 2</td>
<td data-th="Document Date" data-sort="15-Feb-2023" class="cell-width-16p">15-Feb-2023</td>
</tr>
<tr data-href="javascript:void(0);" data-doctype="100.16645" data-sly-test.repoIdHypenDocId="2-2" data-sly-test.repoIdSlashDocId="2/2">
<td data-th="Name" class="cell-width-16p">ACondition 2</td>
<td data-th="Date of Onset" data-sort="14-Feb-2024" class="cell-width-16p">14-Feb-2024</td>
<td data-th="Source Author" class="cell-width-16p">Author 2</td>
<td data-th="Document Date" data-sort="15-Feb-2023" class="cell-width-16p">15-Feb-2023</td>
</tr>
<tr data-href="javascript:void(0);" data-doctype="100.16644" data-sly-test.repoIdHypenDocId="1-1" data-sly-test.repoIdSlashDocId="1/1">
<td data-th="Name" class="cell-width-16p">Condition 1</td>
<td data-th="Date of Onset" data-sort="01-Jan-3000" class="cell-width-16p"></td>
<td data-th="Source Author" class="cell-width-16p">Author 1</td>
<td data-th="Document Date" data-sort="01-Jan-2024" class="cell-width-16p">01-Jan-2024</td>
</tr>
<tr data-href="javascript:void(0);" data-doctype="100.16644" data-sly-test.repoIdHypenDocId="1-1" data-sly-test.repoIdSlashDocId="1/1">
<td data-th="Name" class="cell-width-16p">Condition 1</td>
<td data-th="Date of Onset" data-sort="01-Jan-3000" class="cell-width-16p"></td>
<td data-th="Source Author" class="cell-width-16p">Author 1</td>
<td data-th="Document Date" data-sort="01-Jan-2024" class="cell-width-16p">01-Jan-2024</td>
</tr>
<tr data-href="javascript:void(0);" data-doctype="100.16644" data-sly-test.repoIdHypenDocId="1-1" data-sly-test.repoIdSlashDocId="1/1">
<td data-th="Name" class="cell-width-16p">Condition 1</td>
<td data-th="Date of Onset" data-sort="01-Jan-3000" class="cell-width-16p"></td>
<td data-th="Source Author" class="cell-width-16p">Author 1</td>
<td data-th="Document Date" data-sort="01-Jan-2024" class="cell-width-16p">01-Jan-2024</td>
</tr>
<!-- Add more rows as needed -->
</tbody>
</table>
<script>
(function() {
'use strict';
$(document).ready(function() {
var $tableElement = $('#meds-condition');
var table;
// Initialize DataTable if not already initialized
if ($.fn.DataTable.isDataTable('#meds-condition')) {
table = $tableElement.DataTable();
} else {
table = $tableElement.DataTable({
"order": [[0, 'asc']], // Default sorting by the second column (Date)
});
}
// Store removed rows temporarily
var removedRows = [];
// Custom function to handle row removal, sorting, and re-adding
function handleSpecialRows() {
// Step 1: Collect and remove rows with data-sort="01-Jan-3000"
removedRows = []; // Reset removed rows array
// Collect rows to be removed
table.rows().nodes().to$().each(function() {
var $row = $(this);
var $td = $row.find('td:eq(1)'); // Target the second column (Date)
var dataSort = $td.attr('data-sort');
// Check if data-sort is '01-Jan-3000'
if (dataSort === '01-Jan-3000') {
removedRows.push($row); // Store the row jQuery object
table.row($row).remove(); // Remove the row from DataTable
}
});
// Step 2: Redraw the table to reflect the changes
table.draw();
// Step 3: Append removed rows to the bottom
if (removedRows.length) {
}
}
// Bind custom function to header click and keypress
$(document).on('click keypress', '#meds-condition_wrapper thead tr th:nth-of-type(2)', function(event) {
if (event.type === 'click' || event.which === 13) {
handleSpecialRows();
}
});
});
})();
</script>
</body>
</html>