From 9a3c38d91ceb5bf754cb280911ce8ca568342ca8 Mon Sep 17 00:00:00 2001 From: Sandy Galloway Date: Tue, 28 May 2019 14:36:36 +0000 Subject: [PATCH] New sorting plug in for numeric sorting within strings. - DD420 --- sorting/numString.js | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 sorting/numString.js diff --git a/sorting/numString.js b/sorting/numString.js new file mode 100644 index 0000000..cfb7f96 --- /dev/null +++ b/sorting/numString.js @@ -0,0 +1,63 @@ +/** + * + * This plug in will sort only on the number value that is included anywhere in a Regex. + * This is useful for sorting data which requires some extra context to be included in the table. + * + * @name numString + * @summary Sort with the prefixed word `dt-string The` removed, if present + * @author [Sandy Galloway](http://datatables.net) + * + * + * @example + * //This example shows a mixture of text and number values, with the number at the start of the expression. + * //It is using regex and start and end of expression tags. + * //It will match "5 examples completed." for example. + * $(document).ready( function(){ + * $.fn.dataTable.numString(/^\d+ examples? completed.$/); + * var table = $('#example').DataTable(); + * }) + * + * @example + * //This example shows a mixture of text and number values, with the number at the end of the expression. + * //It is using regex and start and end of expression tags. + * //It will match "Examples left: 67" for example. + * $(document).ready( function(){ + * $.fn.dataTable.numString(/^Examples? left: \d+$/); + * var table = $('#example').DataTable(); + * }) + * + * @example + * //This example shows a mixture of text and number values, with the number in the middle of the expression. + * //It is using regex and no start and end of expression tags. + * //It will match "Only 1 left." for example. + * $(document).ready(function(){ + * $.fn.dataTable.numString(/Only \d+ left./); + * var table = $('example').DataTable(); + * }) + * + */ +$.fn.dataTable.numString = function(format) { + //This is the type detection plug in + $.fn.dataTable.ext.type.detect.unshift(function(data) { + if (typeof data !== "string") { + return null; + } + + if (data.match(format)) { + return "numString-" + format.source; + } + + return null; + }); + + //This is the ordering plug in + $.fn.dataTable.ext.type.order[ + "numString-" + format.source + "-pre" + ] = function(data) { + var num = data.replace(/\D/g, ""); + + return num * 1; + }; + // end plug-in + }; + \ No newline at end of file