How to sort datatable desc order on "DD-MM-YYYY hh:mm a"
How to sort datatable desc order on "DD-MM-YYYY hh:mm a"
Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
Hi,
i am using SharePoint 2019 ( on prem ), with Jsom to retrieve and bind data
using bootstrap with datable to bind with html table, on dropdown change it retrives data to bind with table
Problem:
i have date column which contains index no 1 on datatable ,having format "DD-MM-YYYY hh:mm A".
I have applied sort but its not working on "DD-MM-YYYY hh:mm A", instead working fine on "MM-DD-YYYY hh:mm A".
Solution required: how can i sort it on desc order default on "DD-MM-YYYY hh:mm A" , then allow user to make it asc and desc on column click , as we do usual
Please guide
my code sample:
datefinal = moment(oListItem.get_item('Created')).format('DD-MM-YYYY hh:mm');
newRow = '<tr id=tr_' + oListItem.get_item('ID') + '> <td> <a href="#" onclick="opendoc(' + oListItem.get_item('ID') + ",'DebtDocs','tblPrivatePlace'" + ');" > ' + oListItem.get_item('FileLeafRef') + ' </a> </td> <td>' + datefinal + '</td>
</tr>
// $("#tblPrivatePlace").append(newRow);
var table = $(tablename).DataTable();
table.row("#tr_" + oListItem.get_item('ID')).remove().draw(false);
////
var table = $(tablename).DataTable();
table.row.add($(newRow)).draw();
table.order([1, 'desc']).draw();
Thanks
Answers
I recommend using this plugin for international date sorting:
https://datatables.net/plug-ins/sorting/datetime-moment
no its not working, i have placed whole code,still hard luck to me
code using jsom
$(document).ready(function () {
var oListItems, clientContext, collInvalidDoctype;
Did you add a statement like this to tell Datatables the datetime format?
If you have Datatables 1.12 then this support is built in. See this release note.
If you still need help please post a link to your page or a test case replicating the issue.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
$.fn.dataTable.moment( 'DD-MM-YYYY hh:mm A' ) where to add this ?
on document.ready ? below is load method
$(document).ready(function () {
below code is working to show data on desc order , based on "DD-MM-YYYY hh:mm A"
$(document).ready(function () {
});
Problem : when i click on column to sort, that's not working ,
what to do to let sort works ???
Add it just before you initialize your Datatable. Just like the example at the link rf1234 provided.
Kevin
Is any one can help me out? , still issue not resolved
It has been working fine for me on multiple occasions. I guess you should post a test case as per this forum's rules.
How can anyone help you out without seeing what your code is? Have you included the plugins (moment.js and the ultimate date sorting plugin)?
Using the plugin you need this code before you initialize the Datatable - as Kevin already mentioned above.
Here is an example from my own coding. I have different requirements depending on the page the user is on and depending on the user language. As you can see this is highly flexible.
Yes, simply saying it isn’t working isn’t going to give us the information that we need to be able to help you. A link to a test case showing the problem will give us the information we need to be able to help you.
Allan