How to sort a table first column as date wise ?

How to sort a table first column as date wise ?

gopi.jtech@gmail.comgopi.jtech@gmail.com Posts: 9Questions: 3Answers: 0
edited July 2023 in DataTables

My script to initialize data table is as follows

$('#myTable').DataTable({
            "ordering": true,
            "paging": false,
            "scrollX": false,
            // "responsive": true,
            dom: 'Blfrtip',
            columnDefs: [{
                targets: [0],
                type: 'date',
                render: function(data, type, row, meta) {
                    if (type === 'sort') {
                        // Provide the date format for sorting
                        return moment(data, 'DD/MM/YYYY').valueOf(); // Change the date format according to your data
                    }
                    return data;
                }
            }],
            buttons: [{
                    extend: 'copy',
                    exportOptions: {
                        columns: ':visible'
                    }
                },
                {
                    extend: 'csv',
                    exportOptions: {
                        columns: ':visible'
                    }
                },
                {
                    extend: 'excel',
                    exportOptions: {
                        columns: ':visible'
                    },
                    stripNewlines: false,
                    //messageTop: $("#print_head").html()
                    messageTop: function() {
                        var myHString = $('#print_head').html();
                        var cutString1 = myHString.replace('<pre style="font: 15px Arial, sans-serif;">', '');
                        var cutString2 = cutString1.replace('</pre>', '');
                        // var print_head = $('#print_head').html();
                        // return  $("#print_head").html() ;
                        return cutString2;
                    }
                },

                {
                    extend: 'pdf',
                    exportOptions: {
                        columns: ':visible'
                    },
                    stripNewlines: false,
                    //messageTop: $("#print_head").html()
                    messageTop: function() {
                        var myHString = $('#print_head').html();
                        var cutString1 = myHString.replace('<pre style="font: 15px Arial, sans-serif;">', '');
                        var cutString2 = cutString1.replace('</pre>', '');
                        // var print_head = $('#print_head').html();
                        // return  $("#print_head").html() ;
                        return cutString2;
                    }
                },

                {
                    extend: 'copy',
                    text: 'Print',
                    exportOptions: {
                        columns: ':visible'
                    },
                    action: function(e, dt, button, config) {
                        var data = dt.buttons.exportData({
                            columns: ':visible'
                        });
                        var jsonData = JSON.stringify(data);
                        doPrint(data);
                    }
                }
            ]
        });

I have given

columnDefs: [{
                targets: [0],
                type: 'date',
                render: function(data, type, row, meta) {
                    if (type === 'sort') {
                        // Provide the date format for sorting
                        return moment(data, 'DD/MM/YYYY').valueOf(); // Change the date format according to your data
                    }
                    return data;
                }
            }],

which I got from this forum. but this does not work. Can you help to sort this issue. Thanks in advance.

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Replies

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    If you have Datatables 1.12 or later see this doc. For earlier versions use the solution documented in this blog.

    Kevin

Sign In or Register to comment.