From adbbf3652647704ffc6efadb32d5b995c07cfe39 Mon Sep 17 00:00:00 2001 From: Johannes Wilm Date: Wed, 10 Apr 2024 22:56:44 +0200 Subject: [PATCH] sort numeric rows correctly, fixes #372, fixes #357 --- docs/demos/26-numeric-sort/index.html | 127 ++++++++++++++++++++++++++ docs/demos/index.html | 1 + src/read_data.ts | 10 +- 3 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 docs/demos/26-numeric-sort/index.html diff --git a/docs/demos/26-numeric-sort/index.html b/docs/demos/26-numeric-sort/index.html new file mode 100644 index 00000000..d617ba7d --- /dev/null +++ b/docs/demos/26-numeric-sort/index.html @@ -0,0 +1,127 @@ + + + + + + + Numeric sort - simple-datatables + + + + + + +
+

+ simple-datatables +

+ Documentation + Demos +
+ +

Numeric sort

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Number#
Minus one point five-1.5
Minus one-1
Minus zero point eight-0.8
Minus zero point five-0.5
Minus zero point two-0.2
Zero0
Zero point five0.5
Zero point eight0.8
One1
One poiny five1.5
Two2
Three3
Four4
Five5
Six6
Seven7
Eight8
Nine9
Ten10
Eleven11
+ + + diff --git a/docs/demos/index.html b/docs/demos/index.html index bfa036b5..0c10f68d 100644 --- a/docs/demos/index.html +++ b/docs/demos/index.html @@ -42,6 +42,7 @@

Demos

Column filter button Custom footer Cell attributes + Numeric sort diff --git a/src/read_data.ts b/src/read_data.ts index c3e1b4df..dd36ee05 100644 --- a/src/read_data.ts +++ b/src/read_data.ts @@ -34,7 +34,8 @@ export const readDataCell = (cell: inputCellType, columnSettings : columnSetting break case "number": cellData.text = String(cellData.data as number) - cellData.data = parseInt(cellData.data as string, 10) + cellData.data = parseFloat(cellData.data as string) + cellData.order = cellData.data break case "html": { const node = Array.isArray(cellData.data) ? @@ -83,12 +84,15 @@ const readDOMDataCell = (cell: HTMLElement, columnSettings : columnSettingsType) } break } - case "number": + case "number": { + const data = parseFloat(cell.innerText) cellData = { - data: parseInt(cell.innerText, 10), + data, + order: data, text: cell.innerText } break + } case "boolean": { const data = !["false", "0", "null", "undefined"].includes(cell.innerText.toLowerCase().trim()) cellData = {