Skip to content

Commit d948d7b

Browse files
Kyrylo PylypenkoKyrylo Pylypenko
authored andcommitted
Sortable Paginated Table
1 parent 7f8e727 commit d948d7b

File tree

6 files changed

+230
-152
lines changed

6 files changed

+230
-152
lines changed

content/body/sortable-pagination-table.php

Lines changed: 151 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
1-
<!-- File WIP -->
2-
31
<?php includeStats([
4-
"isForNewBuilds" => false,
5-
"comment" => "You may want to read inform",
2+
"isForNewBuilds" => true,
3+
"comment" =>
4+
'If you are going to use this in a new webpage, please review our <a href="table.php#sticky-header-example">Sticky Table Header Example</a> and decide which solution is better for your use-case.',
65
]); ?>
76
<?php includeStats([
8-
"isNPM" => true,
9-
"comment" =>
10-
'There are NPM modules for both the <a href="sortable-table.php#npm-instructions">Sortable Table</a> and the <a href="pagination-table.php#npm-instructions">Pagination Table</a> Enable Scripts used in this demo.',
7+
"isForNewBuilds" => false,
118
]); ?>
9+
<?php includeStats(["isNPM" => true]); ?>
1210

11+
<p>
12+
If you have a lot of data in a table that you want to present to the user in small, bite-sized chunks, you may want to
13+
use a pagination UI element to do this. The solution presented below is accessible and works for keyboard and screen
14+
reader users.
15+
</p>
1316

17+
<p>
18+
<strong>That said, it is a bit of work for a keyboard and screen reader use to any pagination UI, even if it is
19+
accessible.</strong> It may be easier for users to navigate <a href="table.php#sticky-header-example">a table with a
20+
sticky header
21+
instead</a>. Before you implement this solution (or if you are trying to make an existing pagination component
22+
accessible), weigh the pros and cons between this and the Sticky Header solution.
23+
</p>
1424

15-
<h2 tabindex="-1">Sortable Paginated Table</h2>
25+
<h2 tabindex="-1">Sortable Paginated Table Example</h2>
1626

17-
<div class="enable-example" id="sortable-paginated-table-example">
18-
<div class="pagination deque-table-sortable-group">
19-
<div id="sortable-paginated-table-example__desc--top" class="sr-only">
27+
<div class="enable-example" id="paginated-table-example">
28+
<div class="pagination deque-table-sortable__group">
29+
<div id="pagination-table-example__desc--top" class="sr-only">
2030
<p>
2131
The buttons inside this control allow you to paginate through
2232
the data in the table below, 10 columns at a time.
2333
</p>
2434
</div>
25-
<div id="sortable-paginated-table-example__sort-instructions">
26-
Click the table heading buttons to sort the table by the data in its column.
27-
</div>
28-
<div class="pagination__pager" role="group" aria-labelledby="sortable-paginated-table-example__desc--top"></div>
35+
<div class="pagination__pager" role="group" aria-labelledby="pagination-table-example__desc--top"></div>
2936
<figure>
30-
<figcaption id="sortable-paginated-table-example__caption" class="caption">
37+
<figcaption id="pagination-table-example__caption" class="caption">
3138
Pagination Table Example: GDP of the World Nations
3239
</figcaption>
3340
<div class="sticky-table__container" tabindex="0">
34-
<table role="grid" aria-readonly="true" class="pagination__table deque-table-sortable" data-pagecount="7"
41+
<table class="pagination__table" data-pagecount="7"
3542
data-pagination-alert-template="Now displaying rows ${n} through ${m}" data-pagination-button-spread="5"
36-
data-pagination-mobile-button-spread="4" aria-labelledby="sortable-paginated-table-example__caption"
37-
data-aria-live-update="The table ${caption} is now ${sortedBy}"
38-
data-ascending-label="Sorted in ascending order" data-descending-label="Sorted in descending order">
43+
data-pagination-mobile-button-spread="4"
44+
data-aria-live-update="The table ${caption} is now ${sortedBy}"
45+
data-ascending-label="Sorted in ascending order"
46+
data-descending-label="Sorted in descending order"aria-labelledby="pagination-table-example__caption">
3947
<thead>
4048
<tr>
41-
<th scope="col"><button class="sortableColumnLabel"
42-
aria-describedby="sortable-paginated-table-example__sort-instructions">Rank</button></th>
43-
<th scope="col"><button class="sortableColumnLabel"
44-
aria-describedby="sortable-paginated-table-example__sort-instructions">Name</button></th>
45-
<th data-sortable-tables-compare-func="exampleCustomCompare" scope="col"><button
46-
class="sortableColumnLabel" aria-describedby="sortable-paginated-table-example__sort-instructions">GDP
47-
(IMF '19)</button></th>
48-
<th data-sortable-tables-compare-func="exampleCustomCompare" scope="col"><button
49-
class="sortableColumnLabel" aria-describedby="sortable-paginated-table-example__sort-instructions">GDP
50-
(UN '16)</button></th>
51-
<th data-sortable-tables-compare-func="exampleCustomCompare" scope="col"><button
52-
class="sortableColumnLabel" aria-describedby="sortable-paginated-table-example__sort-instructions">GDP
53-
Per Capita</button></th>
54-
<th data-sortable-tables-compare-func="exampleCustomCompare" scope="col"><button
55-
class="sortableColumnLabel" aria-describedby="sortable-paginated-table-example__sort-instructions"
56-
data-sortable-tables-compare-func="exampleCustomCompare">Population</button></th>
49+
<th scope="col"
50+
data-sort-type="number">
51+
<button class="sortableColumnLabel" aria-describedby="user-info-table__sort-instructions">Rank</button></th>
52+
<th scope="col" data-sort-type="default"> <button class="sortableColumnLabel" aria-describedby="user-info-table__sort-instructions">Name</button></th>
53+
<th scope="col" data-sort-type="number"> <button class="sortableColumnLabel" aria-describedby="user-info-table__sort-instructions">GDP (IMF '19)</button></th>
54+
<th scope="col" data-sort-type="number"> <button class="sortableColumnLabel" aria-describedby="user-info-table__sort-instructions">GDP (UN '16)</button></th>
55+
<th scope="col" data-sort-type="default"> <button class="sortableColumnLabel" aria-describedby="user-info-table__sort-instructions">GDP Per Capita</button></th>
56+
<th scope="col" data-sort-type="number"> <button class="sortableColumnLabel" aria-describedby="user-info-table__sort-instructions">Population</button></th>
5757
</tr>
5858
</thead>
5959
<tbody>
@@ -1413,7 +1413,7 @@ class="sortableColumnLabel" aria-describedby="sortable-paginated-table-example__
14131413
<td>170</td>
14141414
<td>Gambia</td>
14151415
<td>1.87 billion</td>
1416-
<td>985.83 Mn</td>
1416+
<td>985.83 million</td>
14171417
<td>$752</td>
14181418
<td>2,486,945</td>
14191419
</tr>
@@ -1469,111 +1469,111 @@ class="sortableColumnLabel" aria-describedby="sortable-paginated-table-example__
14691469
<td>177</td>
14701470
<td>Saint Kitts And Nevis</td>
14711471
<td>1.12 billion</td>
1472-
<td>909.85 Mn</td>
1472+
<td>909.85 million</td>
14731473
<td>$20,880</td>
14741474
<td>53,544</td>
14751475
</tr>
14761476
<tr>
14771477
<td>178</td>
14781478
<td>Vanuatu</td>
1479-
<td>994.00 Mn</td>
1480-
<td>837.52 Mn</td>
1479+
<td>994.00 million</td>
1480+
<td>837.52 million</td>
14811481
<td>$3,161</td>
14821482
<td>314,464</td>
14831483
</tr>
14841484
<tr>
14851485
<td>179</td>
14861486
<td>Samoa</td>
1487-
<td>960.00 Mn</td>
1488-
<td>822.23 Mn</td>
1487+
<td>960.00 million</td>
1488+
<td>822.23 million</td>
14891489
<td>$4,796</td>
14901490
<td>200,149</td>
14911491
</tr>
14921492
<tr>
14931493
<td>180</td>
14941494
<td>Saint Vincent And The Grenadines</td>
1495-
<td>903.00 Mn</td>
1496-
<td>765.32 Mn</td>
1495+
<td>903.00 million</td>
1496+
<td>765.32 million</td>
14971497
<td>$8,116</td>
14981498
<td>111,263</td>
14991499
</tr>
15001500
<tr>
15011501
<td>181</td>
15021502
<td>Comoros</td>
1503-
<td>773.00 Mn</td>
1503+
<td>773.00 million</td>
15041504
<td>1.15 billion</td>
15051505
<td>$870</td>
15061506
<td>888,451</td>
15071507
</tr>
15081508
<tr>
15091509
<td>182</td>
15101510
<td>Dominica</td>
1511-
<td>590.00 Mn</td>
1512-
<td>581.48 Mn</td>
1511+
<td>590.00 million</td>
1512+
<td>581.48 million</td>
15131513
<td>$8,175</td>
15141514
<td>72,167</td>
15151515
</tr>
15161516
<tr>
15171517
<td>183</td>
15181518
<td>Sao Tome And Principe</td>
1519-
<td>527.00 Mn</td>
1520-
<td>342.78 Mn</td>
1519+
<td>527.00 million</td>
1520+
<td>342.78 million</td>
15211521
<td>$2,359</td>
15221522
<td>223,368</td>
15231523
</tr>
15241524
<tr>
15251525
<td>184</td>
15261526
<td>Tonga</td>
1527-
<td>512.00 Mn</td>
1528-
<td>401.46 Mn</td>
1527+
<td>512.00 million</td>
1528+
<td>401.46 million</td>
15291529
<td>$4,796</td>
15301530
<td>106,760</td>
15311531
</tr>
15321532
<tr>
15331533
<td>185</td>
15341534
<td>Micronesia</td>
1535-
<td>389.00 Mn</td>
1536-
<td>329.90 Mn</td>
1535+
<td>389.00 million</td>
1536+
<td>329.90 million</td>
15371537
<td>$3,346</td>
15381538
<td>116,254</td>
15391539
</tr>
15401540
<tr>
15411541
<td>186</td>
15421542
<td>Palau</td>
1543-
<td>324.00 Mn</td>
1544-
<td>310.25 Mn</td>
1543+
<td>324.00 million</td>
1544+
<td>310.25 million</td>
15451545
<td>$17,833</td>
15461546
<td>18,169</td>
15471547
</tr>
15481548
<tr>
15491549
<td>187</td>
15501550
<td>Marshall Islands</td>
1551-
<td>228.00 Mn</td>
1552-
<td>183.00 Mn</td>
1551+
<td>228.00 million</td>
1552+
<td>183.00 million</td>
15531553
<td>$3,825</td>
15541554
<td>59,610</td>
15551555
</tr>
15561556
<tr>
15571557
<td>188</td>
15581558
<td>Kiribati</td>
1559-
<td>196.00 Mn</td>
1560-
<td>173.66 Mn</td>
1559+
<td>196.00 million</td>
1560+
<td>173.66 million</td>
15611561
<td>$1,615</td>
15621562
<td>121,392</td>
15631563
</tr>
15641564
<tr>
15651565
<td>189</td>
15661566
<td>Nauru</td>
1567-
<td>117.00 Mn</td>
1568-
<td>103.47 Mn</td>
1567+
<td>117.00 million</td>
1568+
<td>103.47 million</td>
15691569
<td>$10,758</td>
15701570
<td>10,876</td>
15711571
</tr>
15721572
<tr>
15731573
<td>190</td>
15741574
<td>Tuvalu</td>
1575-
<td>53.00 Mn</td>
1576-
<td>36.70 Mn</td>
1575+
<td>53.00 million</td>
1576+
<td>36.70 million</td>
15771577
<td>$4,442</td>
15781578
<td>11,931</td>
15791579
</tr>
@@ -1597,7 +1597,7 @@ class="sortableColumnLabel" aria-describedby="sortable-paginated-table-example__
15971597
<td>193</td>
15981598
<td>British Virgin Islands</td>
15991599
<td></td>
1600-
<td>971.24 Mn</td>
1600+
<td>971.24 million</td>
16011601
<td>$31,927</td>
16021602
<td>30,421</td>
16031603
</tr>
@@ -1613,7 +1613,7 @@ class="sortableColumnLabel" aria-describedby="sortable-paginated-table-example__
16131613
<td>195</td>
16141614
<td>Cook Islands</td>
16151615
<td></td>
1616-
<td>290.19 Mn</td>
1616+
<td>290.19 million</td>
16171617
<td>$16,521</td>
16181618
<td>17,565</td>
16191619
</tr>
@@ -1677,7 +1677,7 @@ class="sortableColumnLabel" aria-describedby="sortable-paginated-table-example__
16771677
<td>203</td>
16781678
<td>Montserrat</td>
16791679
<td></td>
1680-
<td>62.05 Mn</td>
1680+
<td>62.05 million</td>
16811681
<td>$12,468</td>
16821682
<td>4,977</td>
16831683
</tr>
@@ -1717,7 +1717,7 @@ class="sortableColumnLabel" aria-describedby="sortable-paginated-table-example__
17171717
<td>208</td>
17181718
<td>Anguilla</td>
17191719
<td></td>
1720-
<td>337.52 Mn</td>
1720+
<td>337.52 million</td>
17211721
<td>$22,327</td>
17221722
<td>15,117</td>
17231723
</tr>
@@ -1741,17 +1741,97 @@ class="sortableColumnLabel" aria-describedby="sortable-paginated-table-example__
17411741
<td>211</td>
17421742
<td>Turks And Caicos Islands</td>
17431743
<td></td>
1744-
<td>917.55 Mn</td>
1744+
<td>917.55 million</td>
17451745
<td>$23,388</td>
17461746
<td>39,231</td>
17471747
</tr>
17481748
</tbody>
17491749
</table>
1750+
<span class="deque-table-sortable__live-region sr-only" aria-live="assertive" data-read-captions="false"></span>
17501751
</div>
17511752
</figure>
17521753

17531754
<div class="pagination__alert sr-only" role="alert" aria-live="polite"></div>
1754-
<span class="deque-table-sortable__live-region sr-only" aria-live="polite" data-read-captions="false">
1755-
</span>
17561755

1757-
</div>
1756+
<div id="pagination-table-example__desc--bottom" class="sr-only">
1757+
<p>
1758+
The buttons inside this control allow you to paginate through
1759+
the data in the table above, 10 columns at a time.
1760+
</p>
1761+
</div>
1762+
<div class="pagination__pager" role="group" aria-labelledby="pagination-table-example__desc--bottom"></div>
1763+
1764+
<template id="pagination__template--button">
1765+
<button class="pagination__pager-item ${isSelectedClass}" data-index="${index}"
1766+
aria-label="Display page ${label} of ${totalPages}" aria-current="${ariaCurrent}">
1767+
${label}
1768+
</button>
1769+
</template>
1770+
1771+
<template id="pagination__template--previous-button">
1772+
<button class="pagination__pager-item pagination__pager-item--previous" ${disabledattr}
1773+
aria-label="Display previous page" data-index=${index}>
1774+
&lt;
1775+
</button>
1776+
</template>
1777+
1778+
<template id="pagination__template--next-button">
1779+
<button class="pagination__pager-item pagination__pager-item--next" ${disabledattr} aria-label="Display next page"
1780+
data-index=${index}>
1781+
&gt;
1782+
</button>
1783+
</template>
1784+
</div>
1785+
</div>
1786+
1787+
<?php includeShowcode("paginated-table-example"); ?>
1788+
1789+
<script type="application/json" id="paginated-table-example-props">
1790+
{
1791+
"replaceHtmlRules": {
1792+
"table": "<!-- Insert table data here -->"
1793+
},
1794+
"steps": [{
1795+
"label": "Use the template HTML instead of hardcoding HTML inside your script",
1796+
"highlight": "%OPENCLOSECONTENTTAG%template",
1797+
"notes": "Hardcoding HTML in scripts is bad for two reasons: It prevents a developer for customizing the controls, and it can also hardcode the human language to a specific language. In this example, we use the <code>template</code> HTML tag to create the HTML fragments for the script. The dynamic content is added using template string style variable syntax(e.g. ${x}). See the next step to see how this is replaced"
1798+
},
1799+
{
1800+
"label": "Use <code>interpolate()</code> to place dynamic content inside the templates.",
1801+
"highlight": "%JS%paginationTables.renderTable ||| interpolate",
1802+
"notes": "The <a href=\"js/shared/interpolate.js\">interpolate function</a> is one that I created. It is based on code from a Stack Overflow page, <a href=\"https://stackoverflow.com/questions/29182244/convert-a-string-to-a-template-string\">Convert a string to a template string</a>, with a few <a href=\"https://gomakethings.com/how-to-sanitize-third-party-content-with-vanilla-js-to-prevent-cross-site-scripting-xss-attacks/\">XSS sanitizing logic included</a>"
1803+
},
1804+
{
1805+
"label": "Mark up the pagination widget correctly",
1806+
"highlight": "role=\"group\" ||| aria-labelledby=\"pagination-table-example__desc--top\" ||| id=\"pagination-table-example__desc--top\" ||| aria-labelledby=\"pagination-table-example__desc--bottom\" ||| id=\"pagination-table-example__desc--bottom\"",
1807+
"notes": "This widget is marked up as a group so screen readers will announce instructions on how it works when they navigate inside of it. Note the instructions are screen reader only using the <a href=\"screen-reader-only-text.php\"><code>sr-only</code></a> class."
1808+
},
1809+
{
1810+
"label": "Ensure all pagination buttons have an aria-label",
1811+
"highlight": "aria-label=\"[^\"]*\"",
1812+
"notes": "This is to ensure the purpose of every button is reported to screen readers, since words like \"Greater than\", \"one\", \"two\", etc., would be confusing"
1813+
},
1814+
{
1815+
"label": "Disable buttons that are not useful to screen reader users",
1816+
"highlight": "\\$\\{disabledattr\\}",
1817+
"notes": "This variable will be set to <code>\"disabled\"</code> if the arrow is supposed to be disabled, and to a blank string if not. This element will be skipped in the tabbing order."
1818+
},
1819+
{
1820+
"label": "Use an ARIA live region to give update information to screen reader users",
1821+
"highlight": "aria-live ||| role=\"alert\"",
1822+
"notes": "This aria-live region will be updated with information for screen reader users on what has changed in the table when the pagination buttons are pressed."
1823+
}
1824+
1825+
]
1826+
}
1827+
</script>
1828+
1829+
<?= includeNPMInstructions(
1830+
"sortable-tables paginate",
1831+
[],
1832+
"deque-table-sortable pagination",
1833+
false,
1834+
[],
1835+
".pagination__table",
1836+
) ?>
1837+

0 commit comments

Comments
 (0)