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] 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