Skip to content

Commit aee9ebe

Browse files
Added an option to format numbers using toLocaleString() for table columns
1 parent b892a72 commit aee9ebe

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

sqlpage/sqlpage.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,22 @@ function table_search_sort(el) {
3535
el,
3636
sort_keys: sort_buttons.map((b, idx) => {
3737
const sort_key = cells[idx]?.textContent;
38-
return { num: Number.parseFloat(sort_key), str: sort_key };
38+
const num = Number.parseFloat(sort_key);
39+
// if the user requested for this column to be formatted using `toLocaleString()`,
40+
// we replace the cell contents
41+
if (cells[idx]?.hasAttribute("number-format-locale") && !Number.isNaN(num)) {
42+
const digits = cell.getAttribute("number-format-digits");
43+
// The variable `digits` can be left empty or contain an integer
44+
const options = digits
45+
? { minimumFractionDigits: digits, maximumFractionDigits: digits }
46+
: {};
47+
// Use the host default language, with the options we just defined
48+
cell.innerHTML = num.toLocaleString(undefined, options);
49+
}
50+
return {
51+
num,
52+
str: sort_key,
53+
};
3954
}),
4055
};
4156
});

sqlpage/templates/table.handlebars

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@
5353
<td class="align-middle {{@key~}}
5454
{{~#if (array_contains_case_insensitive ../../align_right @key)}} text-end {{/if~}}
5555
{{~#if (array_contains_case_insensitive ../../align_center @key)}} text-center {{/if~}}
56-
">
56+
"
57+
{{~#if (array_contains_case_insensitive ../../number_format_locale @key)}} number-format-locale {{/if~}}
58+
number-format-digits={{ ../../number_format_digits }}
59+
>
5760
{{~#if (array_contains_case_insensitive ../../markdown @key)~}}
5861
{{{markdown this}}}
5962
{{~else~}}

0 commit comments

Comments
 (0)