From 2e8488b9e48af0c7a67006b9aeef9f8fcdf0e347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E5=9F=8E=E5=8D=8E?= <1806804825@qq.com> Date: Thu, 22 Dec 2016 17:14:41 +0800 Subject: [PATCH 1/2] When dealing with computer file sizes, it is common to append a post fix such as B, KB, MB or GB to a string in order to easily denote the order of magnitude of the file size. This plug-in allows sorting to take these indicates of size into account. --- sorting/time-elapsed-dhms.js | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 sorting/time-elapsed-dhms.js diff --git a/sorting/time-elapsed-dhms.js b/sorting/time-elapsed-dhms.js new file mode 100644 index 0000000..15e49c1 --- /dev/null +++ b/sorting/time-elapsed-dhms.js @@ -0,0 +1,43 @@ +/** + * Created by caochenghua on 2016/12/22. + */ + +/** + * When dealing with time elapsed, it is common to append a post fix + * such as d(day), h(hour), m(minute) or s(second) to a string in order to easily denote the brief duration + * of the time span from now. This plug-in allows sorting to take these + * indicates of size into account. + * + * A counterpart type detection plug-in is also available. + * + * @name Time span + * @summary Sort abbreviated time span correctly (2d 3h, 2h 8m, 3m 8s, 30s, etc) + * @author Allan Jardine - datatables.net + * + * @example + * $('#example').DataTable( { + * columnDefs: [ + * { type: 'time-elapsed-dhms', targets: 0 } + * ] + * } ); + */ + + + +jQuery.fn.dataTable.ext.type.order['time-elapsed-dhms-pre'] = function(data) { + + var matches = data.match(/^(\d+(?:\.\d+)?)\s*([a-z]+)/i); + var multipliers = { + s: 1, + m: 60, + h: 3600, + d: 86400 + }; + + if (matches) { + var multiplier = multipliers[matches[2].toLowerCase()]; + return parseFloat(matches[1]) * multiplier; + } else { + return -1; + }; +}; \ No newline at end of file From 7c6bfd5c05126ee64deb4c555457ac4c3eb61c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E5=9F=8E=E5=8D=8E?= <1806804825@qq.com> Date: Thu, 22 Dec 2016 17:21:34 +0800 Subject: [PATCH 2/2] * When dealing with time span elapsed, it is common to append a post fix * such as d(day), h(hour), m(minute) or s(second) to a string in order to easily denote the brief duration * of the time span from now. This plug-in allows sorting to take these * indicates of size into account. --- sorting/time-elapsed-dhms.js | 1 - 1 file changed, 1 deletion(-) diff --git a/sorting/time-elapsed-dhms.js b/sorting/time-elapsed-dhms.js index 15e49c1..cd483f5 100644 --- a/sorting/time-elapsed-dhms.js +++ b/sorting/time-elapsed-dhms.js @@ -23,7 +23,6 @@ */ - jQuery.fn.dataTable.ext.type.order['time-elapsed-dhms-pre'] = function(data) { var matches = data.match(/^(\d+(?:\.\d+)?)\s*([a-z]+)/i);