Skip to content

Commit 35daed3

Browse files
authored
Merge pull request #42412 from dipesh-rawat/add-filter-feature-table
Add sortable option to table shortcode
2 parents d725c81 + d97c4a2 commit 35daed3

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

content/en/docs/reference/command-line-tools-reference/feature-gates.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ For a reference to old feature gates that are removed, please refer to
5252

5353
### Feature gates for Alpha or Beta features
5454

55-
{{< table caption="Feature gates for features in Alpha or Beta states" >}}
55+
{{< table caption="Feature gates for features in Alpha or Beta states" sortable="true" >}}
5656

5757
| Feature | Default | Stage | Since | Until |
5858
|---------|---------|-------|-------|-------|
@@ -224,7 +224,7 @@ For a reference to old feature gates that are removed, please refer to
224224

225225
### Feature gates for graduated or deprecated features
226226

227-
{{< table caption="Feature Gates for Graduated or Deprecated Features" >}}
227+
{{< table caption="Feature Gates for Graduated or Deprecated Features" sortable="true">}}
228228

229229
| Feature | Default | Stage | Since | Until |
230230
|---------|---------|-------|-------|-------|

layouts/partials/head.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@
8383
<!--script src="https://unpkg.com/split.js/dist/split.min.js"></script-->
8484
<script src="/js/split-1.6.0.js" intregrity="sha384-0blL3GqHy6+9fw0cyY2Aoiwg4onHAtslAs4OkqZY7UQBrR65/K4gI+hxLdWDrjpz"></script>
8585

86+
{{- if .HasShortcode "table" -}}
87+
<script defer src="{{ "js/sortable-table.js" | relURL }}"></script>
88+
{{- end -}}
89+
8690
{{- if eq (lower .Params.cid) "community" -}}
8791
{{- if eq .Params.community_styles_migrated true -}}
8892
<link href="/css/community.css" rel="stylesheet"><!-- legacy styles -->

layouts/shortcodes/table.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{{ $hasCaption := isset .Params "caption" }}
22
{{ $caption := .Get "caption" }}
3+
{{ $sortable := .Get "sortable" }}
34
{{ $captionEl := printf "<table><caption style=\"display: none;\">%s</caption>" $caption }}
45
{{ $table := .Inner | markdownify }}
56
{{ $html := cond $hasCaption ($table | replaceRE "<table>" $captionEl) $table | safeHTML }}
7+
<!-- Check if 'sortable' is true, and if so, add the 'sortable-table' class -->
8+
{{ if $sortable }}
9+
{{ $html = replaceRE "<table>" "<table class=\"sortable-table\">" $html | safeHTML }}
10+
{{ end }}
611
{{ $html }}

static/js/sortable-table.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
document.addEventListener("DOMContentLoaded", function () {
2+
const tables = document.querySelectorAll(".sortable-table");
3+
tables.forEach((table) => {
4+
const headers = table.querySelectorAll("thead th");
5+
headers.forEach((th, index) => {
6+
th.style.cursor = "pointer";
7+
th.addEventListener("click", () => sortTable(table, index));
8+
});
9+
});
10+
});
11+
12+
function sortTable(table, column) {
13+
const rows = Array.from(table.querySelectorAll("tbody tr"));
14+
let sortOrder = table.getAttribute("data-sort-order") || "asc";
15+
16+
rows.sort((a, b) => {
17+
const aValue = a.querySelectorAll("td")[column].innerText;
18+
const bValue = b.querySelectorAll("td")[column].innerText;
19+
if (sortOrder === "asc") {
20+
return aValue.localeCompare(bValue, undefined, { numeric: true, sensitivity: "base" });
21+
} else {
22+
return bValue.localeCompare(aValue, undefined, { numeric: true, sensitivity: "base" });
23+
}
24+
});
25+
26+
rows.forEach((row) => table.querySelector("tbody").appendChild(row));
27+
28+
sortOrder = (sortOrder === "asc") ? "desc" : "asc";
29+
table.setAttribute("data-sort-order", sortOrder);
30+
}

0 commit comments

Comments
 (0)