{"id":249,"date":"2015-05-13T09:57:16","date_gmt":"2015-05-13T07:57:16","guid":{"rendered":"http:\/\/blog.thecodecampus.de\/?p=249"},"modified":"2015-05-13T09:57:16","modified_gmt":"2015-05-13T07:57:16","slug":"legacy-datatable-sorting-with-moment-js","status":"publish","type":"post","link":"https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/","title":{"rendered":"Legacy DataTable sorting with moment.js"},"content":{"rendered":"<p>Recently I had the requirement to add sorting to a legacy DataTables (1.7.5), the column contains data in <code>[Monthname] YYYY<\/code> and <code>DD.MM.YYYY \u2013 DD.MM.YYYY<\/code> format.<br \/>\nFor example:<\/p>\n<ul>\n<li>December 2014<\/li>\n<li>15.10.14-30.10.14<\/li>\n<li>January 2015<\/li>\n<\/ul>\n<p>The normal <a href=\"http:\/\/datatables.net\/\">DataTables<\/a> sorting algorithm can only sort the column alphabetically, so we needed some improvement. After a short search on the DataTables website, I found this blog entry (<a href=\"http:\/\/datatables.net\/blog\/2014-12-18\" target=\"_blank\">http:\/\/datatables.net\/blog\/2014-12-18<\/a>) with a plugin from Allan Jardin. The plugin can detect the date fields by a given pattern and then sort them with <a href=\"http:\/\/momentjs.com\/\">moment.js<\/a>.<\/p>\n<p>The problem here is, Allan Jardin wrote this plugin for the new DataTables API, so I first had to change the API-Calls to fit the legacy DataTables API. I changed following lines:<\/p>\n<pre class=\"lang:js decode:true\">var types = $.fn.dataTable.ext.type;<\/pre>\n<p>to<\/p>\n<pre class=\"lang:js decode:true\">var types = $.fn.dataTableExt.aTypes;<\/pre>\n<p>and<\/p>\n<pre class=\"lang:js decode:true\">types.detect.unshift( function ( d )<\/pre>\n<p>to<\/p>\n<pre class=\"lang:js decode:true\">types.unshift( function ( d )<\/pre>\n<p>The original implementation uses the <code>-pre<\/code> sort name, this is available from DataTables 1.9+, so I splitted the implementation into <code>-asc<\/code> and <code>-desc<\/code> functions. Now you can add your desired date pattern to initialize the column sorting. With the line<\/p>\n<pre class=\"lang:js decode:true \">$.fn.dataTable.moment( 'MMMM YYYY' );<\/pre>\n<p>prior to your DataTables initialization, the sorting is setup and ready to go. You can build your pattern with the help from the <a href=\"http:\/\/momentjs.com\/docs\/#\/parsing\/string-format\/\" target=\"_blank\">moment.js-docs<\/a>.<\/p>\n<p>Here you can find the code for <a href=\"https:\/\/jsfiddle.net\/34n6c22o\/\" target=\"_blank\">Example 1<\/a>.<\/p>\n<p>With this implementation you can sort columns with the same date pattern, but our goal is to sort a column with mixed date patterns, as described above.<\/p>\n<p>One one hand, we can use the pattern <code>MMMM YYYY<\/code> mentioned above, but which pattern should we use for date-ranges?<br \/>\nIn our implementation the dates are not overlapping, so I decided to use the first part of the date-range for sorting (<code>15.10.14 - 30.10.14<\/code> -&gt; <code>15.10.14<\/code>).<\/p>\n<p>Therefore we got two moment.js patterns here:<\/p>\n<ul>\n<li><code>MMMM YYYY<\/code> for December 2014<\/li>\n<li><code>DD.MM.YY<\/code> for 15.10.14<\/li>\n<\/ul>\n<p>But we need the correct unix timestamp and the correct pattern for each entry,so I wrote wrote the <code>getSpecialValueAndFormat<\/code>-Function.<\/p>\n<pre class=\"lang:js decode:true \">function getSpecialValueAndFormat(value) {\r\n\r\n        \/\/Remove tags, then remove \\n and whitespaces\r\n        value = value.replace ? value.replace(\/\/g, '').replace(\/(\\r\\n|\\n|\\r)\/gm, '').trim() : value;\r\n\r\n        if (\/^[a-zA-Z]\/.test(value.trim())) {\r\n            return {\r\n                value: value,\r\n                format: \"MMMM YYYY\"\r\n            };\r\n        } else {\r\n            value = value.trim().split(\" - \")[0];\r\n            return {\r\n                value: value,\r\n                format: \"DD.MM.YY\"\r\n            };\r\n        }\r\n}<\/pre>\n<ul>\n<li>the function removes unused html-tags and whitespaces. (unfortunately generated by the Scala-Lift-Framework)<\/li>\n<li>based on a regex, the function decides which pattern should be used and returns the value and the correct pattern<\/li>\n<\/ul>\n<p>With the value and the pattern, we can use the <code>parseFormatToUnix<\/code>-Function from the first Example and calculate the timestamps.<\/p>\n<p>Finally you only need to specify the <code>sType<\/code> of the DataTables column with:<\/p>\n<pre class=\"lang:js decode:true \">$('#table').dataTable({\r\n        \"aoColumns\": [{\r\n            sType: \"custom-date\"\r\n        }]\r\n    });\r\n<\/pre>\n<p>Here you can see the final code for <a href=\"https:\/\/jsfiddle.net\/6txbbmc6\/1\/\" target=\"_blank\">Example 2<\/a>.<\/p>\n<p>I hope this helps thanks the moment.js-Team and Allan Jardin for his great plugin.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I had the requirement to add sorting to a legacy DataTables (1.7.5), the column contains data in <code>[Monthname] YYYY<\/code> and <code>DD.MM.YYYY \u2013 DD.MM.YYYY<\/code> format. For example: December 2014 15.10.14-30.10.14 January 2015 The normal DataTables sorting algorithm can only sort the column alphabetically, so we needed some improvement. After a short search on the DataTables [&#8230;]<br \/><a class=\"meta-big\" href=\"https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/\"> READ MORE<\/a><\/p>\n","protected":false},"author":29,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[44,61,40,46,45],"class_list":["post-249","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-datatables","tag-javascript","tag-legacy","tag-moment-js","tag-sorting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Legacy DataTable sorting with moment.js - Web Development Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Legacy DataTable sorting with moment.js - Web Development Blog\" \/>\n<meta property=\"og:description\" content=\"Recently I had the requirement to add sorting to a legacy DataTables (1.7.5), the column contains data in [Monthname] YYYY and DD.MM.YYYY \u2013 DD.MM.YYYY format. For example: December 2014 15.10.14-30.10.14 January 2015 The normal DataTables sorting algorithm can only sort the column alphabetically, so we needed some improvement. After a short search on the DataTables [...] READ MORE\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Development tips and tricks - theCodeCampus Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-05-13T07:57:16+00:00\" \/>\n<meta name=\"author\" content=\"theCodeCampus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"theCodeCampus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/legacy-datatable-sorting-with-moment-js\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/legacy-datatable-sorting-with-moment-js\\\/\"},\"author\":{\"name\":\"theCodeCampus\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#\\\/schema\\\/person\\\/276bbda2f8da73154f22fb652201cfbc\"},\"headline\":\"Legacy DataTable sorting with moment.js\",\"datePublished\":\"2015-05-13T07:57:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/legacy-datatable-sorting-with-moment-js\\\/\"},\"wordCount\":379,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#organization\"},\"keywords\":[\"DataTables\",\"JavaScript\",\"Legacy\",\"moment.js\",\"Sorting\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/legacy-datatable-sorting-with-moment-js\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/legacy-datatable-sorting-with-moment-js\\\/\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/legacy-datatable-sorting-with-moment-js\\\/\",\"name\":\"Legacy DataTable sorting with moment.js - Web Development Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#website\"},\"datePublished\":\"2015-05-13T07:57:16+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/legacy-datatable-sorting-with-moment-js\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/legacy-datatable-sorting-with-moment-js\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/legacy-datatable-sorting-with-moment-js\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Legacy DataTable sorting with moment.js\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/\",\"name\":\"Web Development tips and tricks - theCodeCampus Blog\",\"description\":\"Tips, tricks, and experiences about developing web and mobile applications with Angular, TypeScript, and Testing.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#organization\",\"name\":\"theCodeCampus\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/TCC-Logo-Bildmarke-quadratisch.jpg\",\"contentUrl\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/TCC-Logo-Bildmarke-quadratisch.jpg\",\"width\":156,\"height\":156,\"caption\":\"theCodeCampus\"},\"image\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#\\\/schema\\\/person\\\/276bbda2f8da73154f22fb652201cfbc\",\"name\":\"theCodeCampus\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/TCC-Logo-Bildmarke-quadratisch-96x96.jpg\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/TCC-Logo-Bildmarke-quadratisch-96x96.jpg\",\"contentUrl\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/TCC-Logo-Bildmarke-quadratisch-96x96.jpg\",\"caption\":\"theCodeCampus\"},\"description\":\"Our knowledge is not simply gained through reading - it is trained, tested and constantly being expanded. Because first and foremost, we are all developers at W11K. The know-how that we acquire here as developers, consultants and information architects flows immediately into our training courses and articles for theCodeCampus.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/showcase\\\/thecodecampus\\\/\"],\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Legacy DataTable sorting with moment.js - Web Development Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/","og_locale":"en_US","og_type":"article","og_title":"Legacy DataTable sorting with moment.js - Web Development Blog","og_description":"Recently I had the requirement to add sorting to a legacy DataTables (1.7.5), the column contains data in [Monthname] YYYY and DD.MM.YYYY \u2013 DD.MM.YYYY format. For example: December 2014 15.10.14-30.10.14 January 2015 The normal DataTables sorting algorithm can only sort the column alphabetically, so we needed some improvement. After a short search on the DataTables [...] READ MORE","og_url":"https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/","og_site_name":"Web Development tips and tricks - theCodeCampus Blog","article_published_time":"2015-05-13T07:57:16+00:00","author":"theCodeCampus","twitter_card":"summary_large_image","twitter_misc":{"Written by":"theCodeCampus","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/#article","isPartOf":{"@id":"https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/"},"author":{"name":"theCodeCampus","@id":"https:\/\/www.thecodecampus.de\/blog\/#\/schema\/person\/276bbda2f8da73154f22fb652201cfbc"},"headline":"Legacy DataTable sorting with moment.js","datePublished":"2015-05-13T07:57:16+00:00","mainEntityOfPage":{"@id":"https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/"},"wordCount":379,"commentCount":0,"publisher":{"@id":"https:\/\/www.thecodecampus.de\/blog\/#organization"},"keywords":["DataTables","JavaScript","Legacy","moment.js","Sorting"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/","url":"https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/","name":"Legacy DataTable sorting with moment.js - Web Development Blog","isPartOf":{"@id":"https:\/\/www.thecodecampus.de\/blog\/#website"},"datePublished":"2015-05-13T07:57:16+00:00","breadcrumb":{"@id":"https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.thecodecampus.de\/blog\/legacy-datatable-sorting-with-moment-js\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.thecodecampus.de\/blog\/"},{"@type":"ListItem","position":2,"name":"Legacy DataTable sorting with moment.js"}]},{"@type":"WebSite","@id":"https:\/\/www.thecodecampus.de\/blog\/#website","url":"https:\/\/www.thecodecampus.de\/blog\/","name":"Web Development tips and tricks - theCodeCampus Blog","description":"Tips, tricks, and experiences about developing web and mobile applications with Angular, TypeScript, and Testing.","publisher":{"@id":"https:\/\/www.thecodecampus.de\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.thecodecampus.de\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.thecodecampus.de\/blog\/#organization","name":"theCodeCampus","url":"https:\/\/www.thecodecampus.de\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.thecodecampus.de\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/01\/TCC-Logo-Bildmarke-quadratisch.jpg","contentUrl":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/01\/TCC-Logo-Bildmarke-quadratisch.jpg","width":156,"height":156,"caption":"theCodeCampus"},"image":{"@id":"https:\/\/www.thecodecampus.de\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.thecodecampus.de\/blog\/#\/schema\/person\/276bbda2f8da73154f22fb652201cfbc","name":"theCodeCampus","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/01\/TCC-Logo-Bildmarke-quadratisch-96x96.jpg","url":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/01\/TCC-Logo-Bildmarke-quadratisch-96x96.jpg","contentUrl":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/01\/TCC-Logo-Bildmarke-quadratisch-96x96.jpg","caption":"theCodeCampus"},"description":"Our knowledge is not simply gained through reading - it is trained, tested and constantly being expanded. Because first and foremost, we are all developers at W11K. The know-how that we acquire here as developers, consultants and information architects flows immediately into our training courses and articles for theCodeCampus.","sameAs":["https:\/\/www.linkedin.com\/showcase\/thecodecampus\/"],"url":"https:\/\/www.thecodecampus.de\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/posts\/249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/comments?post=249"}],"version-history":[{"count":27,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/posts\/249\/revisions"}],"predecessor-version":[{"id":278,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/posts\/249\/revisions\/278"}],"wp:attachment":[{"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/media?parent=249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/categories?post=249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/tags?post=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}