もくじ
大量のデータを扱うテーブルを見やすく整理するには、ページネーション(ページ送り)機能が便利です。
このページでは、JavaScriptを使ってテーブルにページネーションを実装する方法を、コピペで使えるサンプルコードとともに分かりやすく解説します。
複数テーブルへの対応やカスタマイズ方法も紹介しているので、ぜひご活用ください。
リンク
リンク
【サンプル】テーブルにページネーションを実装
| 見出し1 | 見出し2 | 見出し3 |
|---|---|---|
| sample1 | sample1 | sample1 |
| sample2 | sample2 | sample2 |
| sample3 | sample3 | sample3 |
| sample4 | sample4 | sample4 |
| sample5 | sample5 | sample5 |
| sample6 | sample6 | sample6 |
| sample7 | sample7 | sample7 |
| sample8 | sample8 | sample8 |
| sample9 | sample9 | sample9 |
| sample10 | sample10 | sample10 |
| sample11 | sample11 | sample11 |
| sample12 | sample12 | sample12 |
| sample13 | sample13 | sample13 |
| sample14 | sample14 | sample14 |
| sample15 | sample15 | sample15 |
| sample16 | sample16 | sample16 |
| sample17 | sample17 | sample17 |
| sample18 | sample18 | sample18 |
| sample19 | sample19 | sample19 |
| sample20 | sample20 | sample20 |
| sample21 | sample21 | sample21 |
| sample22 | sample22 | sample22 |
| sample23 | sample23 | sample23 |
| sample24 | sample24 | sample24 |
| sample25 | sample25 | sample25 |
| sample26 | sample26 | sample26 |
| sample27 | sample27 | sample27 |
| sample28 | sample28 | sample28 |
| sample29 | sample29 | sample29 |
| sample30 | sample30 | sample30 |
| sample31 | sample31 | sample31 |
| sample32 | sample32 | sample32 |
| sample33 | sample33 | sample33 |
| sample34 | sample34 | sample34 |
| sample35 | sample35 | sample35 |
| sample36 | sample36 | sample36 |
| sample37 | sample37 | sample37 |
| sample38 | sample38 | sample38 |
| sample39 | sample39 | sample39 |
| sample40 | sample40 | sample40 |
| sample41 | sample41 | sample41 |
| sample42 | sample42 | sample42 |
| sample43 | sample43 | sample43 |
| sample44 | sample44 | sample44 |
| sample45 | sample45 | sample45 |
| sample46 | sample46 | sample46 |
| sample47 | sample47 | sample47 |
| sample48 | sample48 | sample48 |
| sample49 | sample49 | sample49 |
| sample50 | sample50 | sample50 |
| sample51 | sample51 | sample51 |
| sample52 | sample52 | sample52 |
| sample53 | sample53 | sample53 |
| sample54 | sample54 | sample54 |
| sample55 | sample55 | sample55 |
| sample56 | sample56 | sample56 |
| sample57 | sample57 | sample57 |
| sample58 | sample58 | sample58 |
| sample59 | sample59 | sample59 |
| sample60 | sample60 | sample60 |
| sample61 | sample61 | sample61 |
| sample62 | sample62 | sample62 |
| sample63 | sample63 | sample63 |
| sample64 | sample64 | sample64 |
| sample65 | sample65 | sample65 |
| sample66 | sample66 | sample66 |
| sample67 | sample67 | sample67 |
| sample68 | sample68 | sample68 |
| sample69 | sample69 | sample69 |
| sample70 | sample70 | sample70 |
| sample71 | sample71 | sample71 |
| sample72 | sample72 | sample72 |
| sample73 | sample73 | sample73 |
| sample74 | sample74 | sample74 |
| 見出し1 | 見出し2 | 見出し3 |
|---|---|---|
| sample1 | sample1 | sample1 |
| sample2 | sample2 | sample2 |
| sample3 | sample3 | sample3 |
| sample4 | sample4 | sample4 |
| sample5 | sample5 | sample5 |
| sample6 | sample6 | sample6 |
| sample7 | sample7 | sample7 |
| sample8 | sample8 | sample8 |
| sample9 | sample9 | sample9 |
| sample10 | sample10 | sample10 |
| sample11 | sample11 | sample11 |
| sample12 | sample12 | sample12 |
| sample13 | sample13 | sample13 |
| sample14 | sample14 | sample14 |
| sample15 | sample15 | sample15 |
| sample16 | sample16 | sample16 |
| sample17 | sample17 | sample17 |
| sample18 | sample18 | sample18 |
| sample19 | sample19 | sample19 |
| sample20 | sample20 | sample20 |
| sample21 | sample21 | sample21 |
| sample22 | sample22 | sample22 |
| sample23 | sample23 | sample23 |
| sample24 | sample24 | sample24 |
| sample25 | sample25 | sample25 |
| sample26 | sample26 | sample26 |
| sample27 | sample27 | sample27 |
| sample28 | sample28 | sample28 |
| sample29 | sample29 | sample29 |
| sample30 | sample30 | sample30 |
| sample31 | sample31 | sample31 |
| sample32 | sample32 | sample32 |
| sample33 | sample33 | sample33 |
| sample34 | sample34 | sample34 |
| sample35 | sample35 | sample35 |
| sample36 | sample36 | sample36 |
| sample37 | sample37 | sample37 |
| sample38 | sample38 | sample38 |
| sample39 | sample39 | sample39 |
| sample40 | sample40 | sample40 |
| sample41 | sample41 | sample41 |
| sample42 | sample42 | sample42 |
| sample43 | sample43 | sample43 |
| sample44 | sample44 | sample44 |
| sample45 | sample45 | sample45 |
| sample46 | sample46 | sample46 |
| sample47 | sample47 | sample47 |
| sample48 | sample48 | sample48 |
| sample49 | sample49 | sample49 |
| sample50 | sample50 | sample50 |
| sample51 | sample51 | sample51 |
| sample52 | sample52 | sample52 |
| sample53 | sample53 | sample53 |
| sample54 | sample54 | sample54 |
| sample55 | sample55 | sample55 |
| sample56 | sample56 | sample56 |
| sample57 | sample57 | sample57 |
| sample58 | sample58 | sample58 |
| sample59 | sample59 | sample59 |
| sample60 | sample60 | sample60 |
| sample61 | sample61 | sample61 |
| sample62 | sample62 | sample62 |
| sample63 | sample63 | sample63 |
| sample64 | sample64 | sample64 |
| sample65 | sample65 | sample65 |
| sample66 | sample66 | sample66 |
| sample67 | sample67 | sample67 |
| sample68 | sample68 | sample68 |
| sample69 | sample69 | sample69 |
| sample70 | sample70 | sample70 |
【コピペOK】テーブルにページネーションを追加するための実装コード
Copyをクリックするとコピーできます。
HTML
Copy
<div class="pagination-table-wrapper"> <div class="page-num-wrapper js-page-num-wrapper"></div> <table class="pagination-table js-pagination-table"> <tr class="pagination-table-row"> <th class="pagination-table-col">見出し1</th> <th class="pagination-table-col">見出し2</th> <th class="pagination-table-col">見出し3</th> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample1</td> <td class="pagination-table-col">sample1</td> <td class="pagination-table-col">sample1</td> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample2</td> <td class="pagination-table-col">sample2</td> <td class="pagination-table-col">sample2</td> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample3</td> <td class="pagination-table-col">sample3</td> <td class="pagination-table-col">sample3</td> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample4</td> <td class="pagination-table-col">sample4</td> <td class="pagination-table-col">sample4</td> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample5</td> <td class="pagination-table-col">sample5</td> <td class="pagination-table-col">sample5</td> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample6</td> <td class="pagination-table-col">sample6</td> <td class="pagination-table-col">sample6</td> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample7</td> <td class="pagination-table-col">sample7</td> <td class="pagination-table-col">sample7</td> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample8</td> <td class="pagination-table-col">sample8</td> <td class="pagination-table-col">sample8</td> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample9</td> <td class="pagination-table-col">sample9</td> <td class="pagination-table-col">sample9</td> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample10</td> <td class="pagination-table-col">sample10</td> <td class="pagination-table-col">sample10</td> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample11</td> <td class="pagination-table-col">sample11</td> <td class="pagination-table-col">sample11</td> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample12</td> <td class="pagination-table-col">sample12</td> <td class="pagination-table-col">sample12</td> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample13</td> <td class="pagination-table-col">sample13</td> <td class="pagination-table-col">sample13</td> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample14</td> <td class="pagination-table-col">sample14</td> <td class="pagination-table-col">sample14</td> </tr> <tr class="pagination-table-row"> <td class="pagination-table-col">sample15</td> <td class="pagination-table-col">sample15</td> <td class="pagination-table-col">sample15</td> </tr> </table></div>CSS
Copy
.pagination-table { width: 100%; text-align: center; border-collapse: collapse;}.pagination-table,.pagination-table-col { padding: 10px; border: solid 1px #000;}.page-num-wrapper { margin-bottom: 10px;}.page-num { margin-right: 10px; color: #0000ff; cursor: pointer;}.page-num:hover { border-bottom: 1px solid #0000ff;}.page-num:last-child { margin-right: 0;}.page-num.is-current-page { color: #000; cursor: auto;}.page-num.is-current-page:hover { color: inherit; border: none;}.page-num.is-tots { color: #000;}.page-num.is-tots:hover { color: #000; border: none; cursor: auto;}JavaScript
Copy
function createTablePagination(paginationTable, pageNumWrapper, setting) { const rows = Array.prototype.slice.call(paginationTable.getElementsByClassName('pagination-table-row'), setting.heading); let currentPage = 1; // ページ番号クリックイベント pageNumWrapper.onclick = function(e) { const target = e.target; if (target.className.indexOf('js-page-num') !== -1 && target.className.indexOf('is-current-page') === -1 && target.className.indexOf('is-dots') === -1) { const page = Number(target.getAttribute('data-page')); if (page) { currentPage = page; render(); } } // 「次へ」「前へ」対応 if (target.className.indexOf('is-next-page') !== -1) { if (currentPage < getTotalPages()) { currentPage++; render(); } } if (target.className.indexOf('is-previous-page') !== -1) { if (currentPage > 1) { currentPage--; render(); } } }; function getTotalPages() { return Math.ceil(rows.length / setting.number); } function render() { // 表示・非表示切り替え for (let i = 0; i < rows.length; i++) { rows[i].style.display = (i >= (currentPage - 1) * setting.number && i < currentPage * setting.number) ? '' : 'none'; } // ページ番号描画 pageNumWrapper.innerHTML = createPageNumbers(); } function createPageNumbers() { const total = getTotalPages(); const max = setting.numberDisplayedPages || total; let html = ''; // 「前へ」 if (setting.previousPage && currentPage > 1) { html += '<span class="page-num js-page-num is-previous-page">' + setting.previousPage + '</span>'; } if (setting.numberDisplayedPages === false) { // すべてのページ番号を表示 for (let i = 1; i <= total; i++) { if (i === currentPage) { html += '<span class="page-num js-page-num is-current-page" data-page="' + i + '">' + i + '</span>'; } else { html += '<span class="page-num js-page-num" data-page="' + i + '">' + i + '</span>'; } } } else { // 省略記号ありの通常ロジック const half = Math.floor(max / 2); let start = Math.max(1, currentPage - half); let end = start + max - 1; if (end > total) { end = total; start = Math.max(1, end - max + 1); } if (start > 1) { html += '<span class="page-num js-page-num" data-page="1">1</span>'; if (start > 2) html += '<span class="page-num js-page-num is-dots">···</span>'; } for (let i = start; i <= end; i++) { if (i === currentPage) { html += '<span class="page-num js-page-num is-current-page" data-page="' + i + '">' + i + '</span>'; } else { html += '<span class="page-num js-page-num" data-page="' + i + '">' + i + '</span>'; } } if (end < total) { if (end < total - 1) html += '<span class="page-num js-page-num is-dots">···</span>'; html += '<span class="page-num js-page-num" data-page="' + total + '">' + total + '</span>'; } } // 「次へ」 if (setting.nextPage && currentPage < total) { html += '<span class="page-num js-page-num is-next-page">' + setting.nextPage + '</span>'; } return html; } // 初期表示 render();}// 初期化document.addEventListener('DOMContentLoaded', function() { //設定 必ず配列で指定してください const settings = { heading: [ 1, //テーブルの見出し数(見出しを使用しない場合は「0」) ], number: [ 5, //テーブルに表示する最大のデータ件数(見出し数を除く) ], numberDisplayedPages: [ false, //ページ番号の最大表示数(全部表示する場合は「false」) ], nextPage: [ '次へ', //非表示にする場合は「''」(空文字) ], previousPage: [ '前へ', //非表示にする場合は「''」(空文字) ] }; const paginationTable = document.getElementsByClassName('js-pagination-table'); const pageNumWrapper = document.getElementsByClassName('js-page-num-wrapper'); for (let i = 0; i < paginationTable.length; i++) { paginationTable[i].id = 'js-pagination-table' + (i + 1); pageNumWrapper[i].id = 'js-page-num-wrapper' + (i + 1); createTablePagination(paginationTable[i], pageNumWrapper[i], { heading: settings.heading[i], number: settings.number[i], numberDisplayedPages: settings.numberDisplayedPages[i], nextPage: settings.nextPage[i], previousPage: settings.previousPage[i] }); }});ページネーション付きテーブルの導入手順
以下の手順でページネーション付きテーブルを設置できます。
- HTML・CSS・JavaScriptのコードをそれぞれコピーして、対象のページに貼り付けてください。
- 必要に応じて、
<tr>・<th>・<td>の行や列を編集してください。 - ページ番号をテーブル下に表示したい場合は、
<div class="page-num-wrapper js-page-num-wrapper"></div>を</table>の直後に移動してください。 - 表示件数やページ番号の表示数などを変更したい場合は、JavaScriptの設定部分(106~122行目)の
const settingsを編集してください。 - デザインを変更したい場合は、CSSを編集してください。
ページネーション付きのテーブルを表示する方法
- テーブルの数だけHTMLを貼り付けてください。
- JavaScriptの設定(
settingsの配列)もテーブルの数に合わせて増やしてください。 - 設定が不足していると正しく表示されませんのでご注意ください。
今回、【サンプル】テーブルにページネーションを実装に2つのテーブルを表示していますが、JavaScriptは以下のように設定しています。
//設定 必ず配列で指定してください
const settings = {
heading: [
1, //テーブルの見出し数(見出しを使用しない場合は「0」)
1,
],
number: [
5, //テーブルに表示する最大のデータ件数(見出し数を除く)
10,
],
numberDisplayedPages: [
false, //ページ番号の最大表示数(全部表示する場合は「false」)
3,
],
nextPage: [
'次へ', //非表示にする場合は「''」(空文字)
'Next',
],
previousPage: [
'前へ', //非表示にする場合は「''」(空文字)
'Prev',
]
};
まとめ
- HTML・CSS・JavaScriptをコピペするだけで、ページネーション付きのテーブルが作れます。
- 表示件数やページ番号の表示数は、JavaScriptの
settingsで調整可能です。 - 複数テーブルを設置する場合は、設定の配列の数をテーブル数に合わせて増やしてください。
- デザインのカスタマイズはCSSで行えます。
- 設定が不足していると正しく動作しないので注意しましょう。
リンク
リンク
