Skip to content

Commit

Permalink
sort numeric rows correctly, fixes #372, fixes #357
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswilm committed Apr 10, 2024
1 parent 5ecc04c commit adbbf36
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 3 deletions.
127 changes: 127 additions & 0 deletions docs/demos/26-numeric-sort/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/svg+xml" href="../../favicon.svg">
<title>Numeric sort - simple-datatables</title>
<!-- DataTable Styles -->
<link rel="stylesheet" href="../dist/css/style.css">
<!-- Demo Styles -->
<link rel="stylesheet" href="../demo.css">
</head>
<body>
<header>
<h1>
<a href="../../">simple-datatables</a>
</h1>
<a href="../../documentation">Documentation</a>
<a href="../">Demos</a>
</header>

<h2>Numeric sort</h2>
<table id="demo-table">
<thead>
<tr>
<th>Number</th>
<th>#</th>
</tr>
</thead>
<tbody>
<tr>
<td>Minus one point five</td>
<td>-1.5</td>
</tr>
<tr>
<td>Minus one</td>
<td>-1</td>
</tr>
<tr>
<td>Minus zero point eight</td>
<td>-0.8</td>
</tr>
<tr>
<td>Minus zero point five</td>
<td>-0.5</td>
</tr>
<tr>
<td>Minus zero point two</td>
<td>-0.2</td>
</tr>
<tr>
<td>Zero</td>
<td>0</td>
</tr>
<tr>
<td>Zero point five</td>
<td>0.5</td>
</tr>
<tr>
<td>Zero point eight</td>
<td>0.8</td>
</tr>
<tr>
<td>One</td>
<td>1</td>
</tr>
<tr>
<td>One poiny five</td>
<td>1.5</td>
</tr>
<tr>
<td>Two</td>
<td>2</td>
</tr>
<tr>
<td>Three</td>
<td>3</td>
</tr>
<tr>
<td>Four</td>
<td>4</td>
</tr>
<tr>
<td>Five</td>
<td>5</td>
</tr>
<tr>
<td>Six</td>
<td>6</td>
</tr>
<tr>
<td>Seven</td>
<td>7</td>
</tr>
<tr>
<td>Eight</td>
<td>8</td>
</tr>
<tr>
<td>Nine</td>
<td>9</td>
</tr>
<tr>
<td>Ten</td>
<td>10</td>
</tr>
<tr>
<td>Eleven</td>
<td>11</td>
</tr>
</tbody>
</table>
<script type="module">
import {DataTable} from "../dist/module.js"
window.dt = new DataTable(
"#demo-table",
{
columns: [
{select: 1,
type: "number",
sort: "desc"}
]
}
)
</script>
</body>
</html>
1 change: 1 addition & 0 deletions docs/demos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ <h2>Demos</h2>
<a href="23-column-filter-button/">Column filter button</a>
<a href="24-footer/">Custom footer</a>
<a href="25-cell-attributes/">Cell attributes</a>
<a href="26-numeric-sort/">Numeric sort</a>
</nav>
</body>
</html>
10 changes: 7 additions & 3 deletions src/read_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) ?
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit adbbf36

Please sign in to comment.