From 623c9f82180eb11418acae8f21136b25655b4550 Mon Sep 17 00:00:00 2001 From: AliRezaMohammadiDoost Date: Thu, 21 Jan 2021 10:20:37 +0330 Subject: [PATCH 1/2] convert numbers to other languages in datatables --- type-detection/numberTo.js | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 type-detection/numberTo.js diff --git a/type-detection/numberTo.js b/type-detection/numberTo.js new file mode 100644 index 0000000..bd7ebef --- /dev/null +++ b/type-detection/numberTo.js @@ -0,0 +1,50 @@ +/** + * convert numbers to farsi, english, arabic. + * تبدیل عداد به فارسی, انگلیسی, عربی + * + * @name convertTo + * @summary convert numbers to farsi, english, arabic. + * @author [alireza mohammadi doost](alirezamohammadi1990@gmail.com) + * @version 0.1 + * + * @example + * $('#example').DataTable( { + * columnDefs: [ + * { + * data: 'id', + * name: 'id', + * fnCreatedCell: function (nTd, sData, oData, iRow, iCol) { + * $(nTd).html($.fn.dataTable.numberTo(oData.id, 'fa')); + * } + * }, + * ] + * } ); + */ + +$(function () { + $.fn.dataTable.numberTo = function (numbers, to = 'fa') { + let result = null; + const faNumbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']; + const enNumbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; + const arNumbers = ['۰', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩']; + + if (!numbers && to === 'fa') { + return 'مقدار ورودی صحیح نمی‌باشد.' + } else if (!numbers) { + return 'numbers is empty.' + } + + switch (to) { + case 'fa': + result = numbers.toString().replace(/\d/g, x => faNumbers[x]); + break; + case 'en': + result = numbers.toString().replace(/\d/g, x => enNumbers[x]); + break; + case 'ar': + result = numbers.toString().replace(/\d/g, x => arNumbers[x]); + break; + } + return result; + }; +}) \ No newline at end of file From 0ab43ae83a39aabc0b3e1ebd8fb33dff2a9d7660 Mon Sep 17 00:00:00 2001 From: SandyDatatables Date: Fri, 5 Feb 2021 16:41:19 +0000 Subject: [PATCH 2/2] new: Luxon version of the Ultimate Date Sorting Plugin Jira Issue DD-1841 --- sorting/datetime-luxon.js | 74 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 sorting/datetime-luxon.js diff --git a/sorting/datetime-luxon.js b/sorting/datetime-luxon.js new file mode 100644 index 0000000..9baf91f --- /dev/null +++ b/sorting/datetime-luxon.js @@ -0,0 +1,74 @@ +/** + * This plug-in for DataTables represents the ultimate option in extensibility + * for sorting date / time strings correctly. It uses + * [luxon](https://moment.github.io/luxon/) to create automatic type detection and + * sorting plug-ins for DataTables based on a given format. This way, DataTables + * will automatically detect your temporal information and sort it correctly. + * + * For usage instructions, please see the DataTables blog + * post that [introduces it](//datatables.net/blog/2014-12-18). + * + * @name Ultimate Date / Time sorting + * @summary Sort date and time in any format using luxon + * @author [Allan Jardine](//datatables.net) + * @depends DataTables 1.10+, luxon.js 1.0+ + * + * @example + * $.fn.dataTable.luxon( 'HH:mm MMM d, yy' ); + * $.fn.dataTable.luxon( 'EEE, MMMM Do, yyyy' ); + * + * $('#example').DataTable(); + */ + +(function (factory) { + if (typeof define === "function" && define.amd) { + define(["jquery", "luxon", "datatables.net"], factory); + } else { + factory(jQuery, luxon); + } +}(function ($, luxon) { + +function strip (d) { + if ( typeof d === 'string' ) { + // Strip HTML tags and newline characters if possible + d = d.replace(/(<.*?>)|(\r?\n|\r)/g, ''); + + // Strip out surrounding white space + d = d.trim(); + } + + return d; +} + +$.fn.dataTable.luxon = function ( format, locale, reverseEmpties ) { + var types = $.fn.dataTable.ext.type; + + // Add type detection + types.detect.unshift( function ( d ) { + d = strip(d); + + // Null and empty values are acceptable + if ( d === '' || d === null ) { + return 'luxon-'+format; + } + + if(!luxon.DateTime.fromFormat( d, format).isValid) { + console.log("pause") + } + + return luxon.DateTime.fromFormat( d, format).isValid ? + 'luxon-'+format : + null; + } ); + + // Add sorting method - use an integer for the sorting + types.order[ 'luxon-'+format+'-pre' ] = function ( d ) { + d = strip(d); + + return !luxon.DateTime.fromFormat(d, format).isValid ? + (reverseEmpties ? -Infinity : Infinity) : + parseInt( luxon.DateTime.fromFormat( d, format).ts, 10 ); + }; +}; + +})); \ No newline at end of file