JavaScript Table Pagination with Search

JavaScript Table Pagination with Search
Code Snippet:Search & Pagination in Pure JavaScript
Author: Delano
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 11,648
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create table pagination with a search box. Basically, it arranges data in a table look-like unordered list. It appends a search box and pagination with that list to filter and navigate table data.

How to Create Table Pagination with Search

1. First of all, create the HTML structure as follows:

  1. <div class="page">
  2. <div class="page-header cf">
  3. <h2>Students</h2>
  4. </div>
  5. <ul class="student-list">
  6. <li class="student-item cf">
  7. <div class="student-details">
  8. <img class="avatar" src="https://randomuser.me/api/portraits/thumb/women/67.jpg">
  9. <h3>iboya vat</h3>
  10. <span class="email">iboya.vat@example.com</span>
  11. </div>
  12. <div class="joined-details">
  13. <span class="date">Joined 07/15/15</span>
  14. </div>
  15. </li>
  16. .
  17. .
  18. .
  19. <li class="student-item cf">
  20. <div class="student-details">
  21. <img class="avatar" src="https://randomuser.me/api/portraits/thumb/men/89.jpg">
  22. <h3>henri kruse</h3>
  23. <span class="email">henri.kruse@example.com</span>
  24. </div>
  25. <div class="joined-details">
  26. <span class="date">Joined 05/14/13</span>
  27. </div>
  28. </li>
  29. </ul>
  30. </div>

2. After that, style the basic page layout and table element using the following CSS code.

  1. .page{
  2. margin: 50px auto;
  3. width: 70%;
  4. background-color: #fff;
  5. border-radius: 5px;
  6. padding: 50px;
  7. }
  8.  
  9. .page-header{
  10. margin-bottom: 20px;
  11. }
  12. .page-header h2{
  13. float: left;
  14. font-size: 22px;
  15. text-transform: uppercase;
  16. font-weight: bold;
  17. color: #555;
  18. }
  19.  
  20. .page-header .student-search{
  21. float: right;
  22. }
  23.  
  24. .page-header .student-search input{
  25. border-radius: 5px;
  26. border: 1px solid #eaeaea;
  27. padding: 8px 15px;
  28. font-size: 14px;
  29. }
  30.  
  31. .page-header .student-search button{
  32. border-radius: 5px;
  33. border: 1px solid #eaeaea;
  34. padding: 8px 15px;
  35. font-size: 14px;
  36. background-color: #4ba6c3;
  37. color: #fff
  38. }
  39.  
  40. .student-list{}
  41.  
  42. .student-item{
  43. margin: 0 0 20px 0;
  44. padding: 0 0 20px 0;
  45. border-bottom: 1px solid #eaeaea;
  46. }
  47.  
  48. .student-details{
  49. width: 50%;
  50. float: left;
  51. }
  52.  
  53. .student-details .avatar{
  54. width: 40px;
  55. height: auto;
  56. border-radius: 20px;
  57. float: left;
  58. margin-right: 14px
  59. }
  60.  
  61. .student-details h3{
  62. margin: 4px 0 2px 0;
  63. font-weight: bold;
  64. color: #4ba6c3;
  65. }
  66.  
  67. .student-details .email{
  68. color: #888;
  69. font-size: 14px;
  70. }
  71.  
  72.  
  73. .joined-details{
  74. width: 50%;
  75. float: left;
  76. text-align: right;
  77. }
  78.  
  79. .joined-details .date{
  80. margin-top: 15px;
  81. display: block;
  82. font-size: 14px;
  83. color: #999;
  84. }
  85.  
  86. .student-item:last-child{
  87. margin: 0;
  88. padding: 0;
  89. border-bottom: none;
  90. }
  91.  
  92. .pagination{
  93. margin: 40px 0 0 0;
  94. text-align: center;
  95. }
  96. .pagination li{
  97. display: inline;
  98. }
  99.  
  100. .pagination li a{
  101. border: 1px solid #eaeaea;
  102. border-radius: 5px;
  103. padding: 3px 8px;
  104. text-decoration: none;
  105. color: #4ba6c3;
  106. }
  107.  
  108. .pagination li a.active,
  109. .pagination li a:hover{
  110. background-color: #4ba6c3;
  111. color: #fff;
  112. }
  113. /* https://meyerweb.com/eric/tools/css/reset/
  114. v2.0 | 20110126
  115. License: none (public domain)
  116. */
  117.  
  118. html, body, div, span, applet, object, iframe,
  119. h1, h2, h3, h4, h5, h6, p, blockquote, pre,
  120. a, abbr, acronym, address, big, cite, code,
  121. del, dfn, em, img, ins, kbd, q, s, samp,
  122. small, strike, strong, sub, sup, tt, var,
  123. b, u, i, center,
  124. dl, dt, dd, ol, ul, li,
  125. fieldset, form, label, legend,
  126. table, caption, tbody, tfoot, thead, tr, th, td,
  127. article, aside, canvas, details, embed,
  128. figure, figcaption, footer, header, hgroup,
  129. menu, nav, output, ruby, section, summary,
  130. time, mark, audio, video {
  131. margin: 0;
  132. padding: 0;
  133. border: 0;
  134. font-size: 100%;
  135. font: inherit;
  136. vertical-align: baseline;
  137. }
  138. /* HTML5 display-role reset for older browsers */
  139. article, aside, details, figcaption, figure,
  140. footer, header, hgroup, menu, nav, section {
  141. display: block;
  142. }
  143. body {
  144. line-height: 1;
  145. }
  146. ol, ul {
  147. list-style: none;
  148. }
  149. blockquote, q {
  150. quotes: none;
  151. }
  152. blockquote:before, blockquote:after,
  153. q:before, q:after {
  154. content: '';
  155. content: none;
  156. }
  157. table {
  158. border-collapse: collapse;
  159. border-spacing: 0;
  160. }
  161.  
  162. .cf:after {
  163. content: ".";
  164. visibility: hidden;
  165. display: block;
  166. height: 0;
  167. clear: both;
  168. }
  169.  
  170. .navlink {
  171. float: left;
  172. }

3. Finally, add the following JavaScript code to your project to functionalize the table search and pagination.

  1. //get all the main elements on the page
  2. var page = document.querySelector(".page");
  3. var pageHeader = document.querySelector(".page-header");
  4. var studentList = document.querySelector(".student-list");
  5. var eachStudent = document.querySelectorAll(".student-item");
  6. var studentDetails = document.querySelector(".student-details");
  7. var inputString = document.getElementById('inputSearch');
  8.  
  9. //Set the pages variable
  10. var currentPage = 1;
  11. var numPages = 0;
  12. var studentsPerPage = 10;
  13. var index;
  14.  
  15. //Recreate Search Element in Js
  16. var searchBar = function createBar (searchString) {
  17. //Creating the three elements that make up the search bar
  18. var studentSearch = document.createElement("div");
  19. var input = document.createElement("input");
  20. var searchButton = document.createElement("button");
  21. var txtNode = document.createTextNode("Search");
  22. //Define the type of the entry
  23. input.type="text";
  24. //Set the attributes fro the different elements to make it easier to select them
  25. studentSearch.setAttribute("class", "student-search");
  26. input.setAttribute("id", "inputSearch");
  27. searchButton.setAttribute("id", "search-button");
  28. //Append all these elements to the page
  29. searchButton.appendChild(txtNode);
  30. studentSearch.appendChild(input);
  31. studentSearch.appendChild(searchButton);
  32. //Setting the placeholder for the searchButton
  33. input.placeholder = "Type name here..";
  34.  
  35. //Return the main element which contains all the elements
  36. return studentSearch;
  37. }
  38.  
  39. //Create static container for pagination in Js
  40. var paginationFilter = function pageFilter (nbOfEntries) {
  41. //Create the static elements for the pagination
  42. var pagination = document.createElement('div');
  43. var ulList = document.createElement('ul');
  44. //Giving them attributes to select them easily
  45. pagination.setAttribute("class", "pagination");
  46. ulList.setAttribute("id", "pagelist");
  47. //Append the ulList to the main pagination div
  48. pagination.appendChild(ulList);
  49. //Return the main div that contains all the elements
  50. return pagination;
  51. };
  52.  
  53. //Finding the number of students
  54. var numberOfStudents = function () {
  55. var numberOfStudents = eachStudent.length;
  56. //Return the number of students
  57. return (numberOfStudents);
  58. }
  59.  
  60. //Finding the number of pages
  61. var numberOfPages = function () {
  62. //Divise the number of students by the number of students per pag
  63. var numberOfPages = parseInt(numberOfStudents() / studentsPerPage);
  64. //If there is a remaining to the above division then create an extra page
  65. if ( numberOfStudents() % studentsPerPage > 0 ){
  66. numPages += 1;
  67. }
  68. //Return the number of students
  69. return numberOfPages;
  70. };
  71.  
  72. //Start by hiding all the students on the list
  73. var hideAll = function () {
  74. for (var i = 0; i < numberOfStudents(); i++) {
  75. eachStudent[i].style.display = "none";
  76. }
  77. };
  78.  
  79. //Then display only 10 intems per page
  80. function showStudents (number) {
  81. for (var i = 0; i < studentsPerPage; i++) {
  82. index = number * studentsPerPage - studentsPerPage + i;
  83. eachStudent[index].style.display = "block";
  84. }
  85. };
  86.  
  87. //Create the pagination links dynamically
  88. function createPages () {
  89.  
  90. for (var i = 0; i <= numberOfPages(); i++) {
  91. //Create the dynamic elements in the pagination
  92. var liList = document.createElement('li');
  93. var pageLink = document.createElement('a');
  94. //Set attributes to the pagination links & the li list
  95. pageLink.setAttribute("href", "#");
  96. pageLink.setAttribute("class", "link");
  97. liList.setAttribute("class", "pageLi");
  98. //Append the different elements to the static ulList
  99. var pagelist = document.getElementById("pagelist");
  100. pagelist.appendChild(liList);
  101. liList.appendChild(pageLink);
  102. //Create and appending the page numbers for the pagination
  103. var pageNumbers = document.createTextNode(i + 1);
  104. pageLink.appendChild(pageNumbers);
  105.  
  106. pageLink.addEventListener("click", function(){
  107. var pageNumClicked = (this.innerHTML || this.innerText);
  108. changePage(pageNumClicked);
  109. }, false);
  110. }
  111. return i;
  112. };
  113.  
  114. //Create the seach feature
  115. var searchFunction = function searchFeature(searchString) {
  116. var eachStudentI;
  117. console.log("Is my search feature working?");
  118. //Get the value entered in the search box
  119. var inputString = document.getElementById('inputSearch');
  120. //Onkeyup we want to filter the content by the string entered in the search box dynamically
  121. inputString.onkeyup = function() {
  122. //toUpperCase to make it case insensitive
  123. var filter = inputString.value.toUpperCase();
  124. //loop through all the li's
  125. for (var i = 0; i < eachStudent.length; i++) {
  126. //Select the student name and retrieve the .innerText value
  127. var studentName = document.getElementsByTagName("h3");
  128. var studentInfo = studentName[i].innerText;
  129. //Display all the results where indexOf() does not return -1
  130. if (studentInfo.toUpperCase().indexOf(filter) != -1) {
  131. eachStudent[i].style.display = 'list-item';
  132. //numberOfStudents = eachStudent[i]
  133. console.log(eachStudent);
  134. } else {
  135. eachStudent[i].style.display = 'none';
  136. }
  137. }
  138. }
  139. }
  140.  
  141. //Global function to add all the elements onLoad of the page
  142. function addElements() {
  143.  
  144. pageHeader.appendChild(searchBar());
  145. page.appendChild(paginationFilter());
  146.  
  147. var searchButton = document.getElementById("search-button");
  148. searchButton.addEventListener("click", searchFunction);
  149. //fadeIn();
  150. //noResults();
  151. hideAll()
  152. createPages();
  153. showStudents(1);
  154. searchFunction();
  155. }
  156. window.onload = addElements();

That’s all! hopefully, you have successfully integrated this JavaScript table search plugin. If you have any questions or facing any issues, feel free to comment below.

1 thought on “JavaScript Table Pagination with Search”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X