Thursday, 20 February 2020

Datatable date sorting issue type dd/mm/yyyy

/////////// There are multiple types of the features in the jquery -

------- Js Plugin --
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
            "date-eu-pre": function (date) {
                date = date.replace(" ", "");

                if (!date) {
                    return 0;
                }

                var year;
                var eu_date = date.split(/[\.\-\/]/);

                /*year (optional)*/
                if (eu_date[2]) {
                    year = eu_date[2];
                }
                else {
                    year = 0;
                }

                /*month*/
                var month = eu_date[1];
                if (month.length == 1) {
                    month = 0 + month;
                }

                /*day*/
                var day = eu_date[0];
                if (day.length == 1) {
                    day = 0 + day;
                }

                return (year + month + day) * 1;
            },

            "date-eu-asc": function (a, b) {
                return ((a < b) ? -1 : ((a > b) ? 1 : 0));
            },

            "date-eu-desc": function (a, b) {
                return ((a < b) ? 1 : ((a > b) ? -1 : 0));
            }

        });

--------- Call Function--

     //// Example
     // $('#example').dataTable( {
     //   columnDefs: [
     //     { type: 'date-eu', targets: 0 }
     //   ]

     //});

Sequence----

1. == >  Extension
2. == >  Datatable.html
3. == >  Datatable.js

No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...