Sorting by Date
Sorting by Date
Hello,
I am using bootstrap popover to show data in my datatable. The data in the popover is a Date and my actual column value is also a Date. Sorting is not working correctly on Ship Dt column. Instead of sorting it by column value, it is sorting it by the first attribute value in the html(please see bolded date in the code snippet and image below). It is sorting it by the first date that it finds in the html.
<td class="sorting_1">
<a class="acronym" tabindex="4" data-bs-toggle="popover" data-bs-placement="bottom" data-bs-html="true" title="" data-bs-content="<p><strong>Order:</strong> 01/19/22 </p><p><strong>Cancel:</strong> 06/01/22</p>" data-bs-original-title="Dates">04/19/22</a>
</td>
However, if the html doesn't have any dates in it then it sorts by the column value(please see code snippet and image below).
<td class="sorting_1">
<a class="acronym" tabindex="1" data-bs-toggle="popover" data-bs-placement="bottom" data-bs-content="Carly Kohut" data-bs-original-title="" title="">DHG00007</a>
</td>
This popover is a requirement in our system. But we also want datatables to sort the date column by the column value instead of html attributes. Is there a way I can do this?
Thanks so much.
Replies
We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin
Hi Colin,
Here is the link to test case:
http://live.datatables.net/koxirehu/1/edit
Thanks,
Asma
Use Orthogonal data to manipulate the data used for the sorting process, you might need to also manipulate the type detection. You will need to extract the date from the data which has HTML tags to sort it correctly. See this updated example:
http://live.datatables.net/koxirehu/2/edit
If the dates don't sort correctly and sort like they are a string then use the technique in this blog to set the format used for sorting.
Kevin