DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Utilizing Database Hooks Like a Pro in Node.js
  • My 7 Must-Have Tools for JavaScript Pros That I Can’t Live Without in 2023
  • A Beginner's Guide to Back-End Development
  • DZone Community Awards 2022

Trending

  • Understanding the Shift: Why Companies Are Migrating From MongoDB to Aerospike Database?
  • Software Delivery at Scale: Centralized Jenkins Pipeline for Optimal Efficiency
  • Advancing Robot Vision and Control
  • A Guide to Auto-Tagging and Lineage Tracking With OpenMetadata
  1. DZone
  2. Data Engineering
  3. Databases
  4. JavaScript: Building Table Sorting and Pagination

JavaScript: Building Table Sorting and Pagination

Adding sorting and pagination to tables in JavaScript.

By 
Raymond Camden user avatar
Raymond Camden
·
Mar. 19, 22 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
3.6K Views

Join the DZone community and get the full member experience.

Join For Free

As part of my job in managing this blog, I check my stats frequently, and I've noticed that some of my more basic Vue.js articles have had consistently good traffic for quite some time. As I find myself doing more and more with "regular" JavaScript (sometimes referred to as "Vanilla JavaScript", but I'm not a fan of the term) I thought it would be a good idea to update those old posts for folks who would rather skip using a framework. With that in mind, here is my update to my post from over four years ago, Building Table Sorting and Pagination in Vue.js

You don't need to read that old post as I think the title is rather descriptive. How can we use JavaScript to take a set of data - render it in a table - and support both sorting and pagination? Here's how I solved this. Before I begin, a quick note. I'll be loading all of my data and while that works with a "sensible" amount of data, you don't want to be sending hundreds of thousands of rows of data to the client and sorting and paging in the client. That being said, I think an entirely client-side solution is absolutely safe if you understand the size of your data and know it's not going to impact performance.

Speaking of data, all of my examples will be fetching an array of cats from this endpoint: https://www.raymondcamden.com/.netlify/functions/get-cats

Here's a sample of that data:

JSON
 
[
  {
    "name": "Fluffy",
    "age": 9,
    "breed": "calico",
    "gender": "male"
  },
  {
    "name": "Luna",
    "age": 10,
    "breed": "long hair",
    "gender": "female"
  },
  {
    "name": "Cracker",
    "age": 8,
    "breed": "fat",
    "gender": "male"
  }
]


Alright, let's get started!

Version One - Just Rendering

In the first version, I'm just going to load the data and render it in a table. I began by creating an HTML table:

HTML
 
<table id="catTable">
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>Breed</th>
      <th>Gender</th>
    </tr>
  </thead>
  <tbody>
    <tr><td colspan="4"><i>Loading...</i></td></tr>
  </tbody>
</table>


Note that the tbody has a loading message. This will render while the remote data is fetched and be replaced with the contents. Now let's look at the JavaScript:

JavaScript
 
document.addEventListener('DOMContentLoaded', init, false);

async function init() {
  
  // Select the table (well, tbody)
  let table = document.querySelector('#catTable tbody');
  // get the cats
  let resp = await fetch('https://www.raymondcamden.com/.netlify/functions/get-cats');
  let data = await resp.json();
  // create html
  let result = '';
  data.forEach(c => {
     result += `<tr>
     <td>${c.name}</td>
     <td>${c.age}</td>
     <td>${c.breed}</td>
     <td>${c.gender}</td>
     </tr>`;
  });
  table.innerHTML = result;
}


This boils down to:

  • Wait for the page to load (this fires before images, stylesheets, and so forth are done though, more info on MDN)
  • Select the tbody element
  • Hit my function and gets the cats
  • Loop over each cat and generate HTML
  • Inject the HTML into the table

Note that while I named my variable table, it's really just the tbody I'm updating. Part of me doesn't like that and part of me thinks I'm being too picky. Guess who won? Here's the complete demo:

See the Pen  JS-Sortable Table by Raymond Camden (@cfjedimaster) on CodePen.

Version Two - Sorting

For the next version, let's add sorting. Sorting will be enabled by clicking on a table header. Clicking once will sort in one direction, clicking again will reverse the sort. First, I modified my table to include data attributes related to the column I'm sorting:

HTML
 
<table id="catTable">
  <thead>
    <tr>
      <th data-sort="name">Name</th>
      <th data-sort="age">Age</th>
      <th data-sort="breed">Breed</th>
      <th data-sort="gender">Gender</th>
    </tr>
  </thead>
  <tbody>
    <tr><td colspan="4"><i>Loading...</i></td></tr>
  </tbody>
</table>


Data attributes are a great way to include metadata in your HTML and - as always - you can learn more at MDN.

To handle sorting, I did a few things. First, I created variables to represent the current sort column and direction. I placed these with a few other values I'll need up top:

JavaScript
 
let data, table, sortCol;
let sortAsc = false;


Next, I added a click event handler to my header cells. I needed to do one per cell:

JavaScript
 
document.querySelectorAll('#catTable thead tr th').forEach(t => {
    t.addEventListener('click', sort, false);
});


As I realized I was going to be redrawing my table a lot, I built a new function to handle rendering the table.

JavaScript
 
function renderTable() {
  // create html
  let result = '';
  data.forEach(c => {
     result += `<tr>
     <td>${c.name}</td>
     <td>${c.age}</td>
     <td>${c.breed}</td>
     <td>${c.gender}</td>
     </tr>`;
  });
  table.innerHTML = result;
}


Finally, I added the sort function. This handles sorting based on both the column and direction:

JavaScript
 
function sort(e) {
  let thisSort = e.target.dataset.sort;
  if(sortCol === thisSort) sortAsc = !sortAsc;
  sortCol = thisSort;
  data.sort((a, b) => {
    if(a[sortCol] < b[sortCol]) return sortAsc?1:-1;
    if(a[sortCol] > b[sortCol]) return sortAsc?-1:1;
    return 0;
  });
  renderTable();
}


I'm not a huge fan of ternary expressions as I find them hard to read at times, but it did make the code a bit simpler above. As I'm not using Vue anymore I have to manually call renderTable again, but that's fine. Here's this version:

See the Pen  JS-Sortable Table (2) by Raymond Camden (@cfjedimaster) on CodePen.

One thing I went back and forth on is whether or not I should apply a default sort. I decided not to, but I could definitely seeing doing that.

Version Three - Paging

Alright, for the third and final version, we need to add paging. My dataset isn't terribly large, so I went with a page size of 3 cats per page. Here is a completely unnecessary picture of three cats:

Three kittens

I started off in HTML by adding two new buttons:

HTML
 
<button id="prevButton">Previous</button> 
<button id="nextButton">Next</button> 


That's it for the HTML changes. Now let's look at the JavaScript. I began by adding a few extra variables related to paging:

JavaScript
 
const pageSize = 3;
let curPage = 1;


Then I added click handlers for the buttons:

JavaScript
 
document.querySelector('#nextButton').addEventListener('click', nextPage, false);
document.querySelector('#prevButton').addEventListener('click', previousPage, false);


Both of these functions need to do basic bounds checking and tell the table to re-render:

JavaScript
 
function previousPage() {
  if(curPage > 1) curPage--;
  renderTable();
}

function nextPage() {
  if((curPage * pageSize) < data.length) curPage++;
  renderTable();
}


That logic in nextPage was a pain in the rear to get right, but luckily I was able to just use the logic from the previous blog post. So where is the paging happening? I do my slicing in the render:

JavaScript
 
function renderTable() {
  // create html
  let result = '';
  data.filter((row, index) => {
        let start = (curPage-1)*pageSize;
        let end =curPage*pageSize;
        if(index >= start && index < end) return true;
  }).forEach(c => {
     result += `<tr>
     <td>${c.name}</td>
     <td>${c.age}</td>
     <td>${c.breed}</td>
     <td>${c.gender}</td>
     </tr>`;
  });
  table.innerHTML = result;
}


Unlike sort which modifies an array in place, filter returns a new array which means I can use it here to get my page of cats and then render that to HTML. Here's that demo:

See the Pen  JS-Sortable Table (3) by Raymond Camden (@cfjedimaster) on CodePen.

This could be enhanced by adding disabled attributes to the button when at the edge. Feel free to fork my CodePen and show me! As always, I hope this is helpful and if you've got any feedback, just reach out!

Database JavaScript Sorting

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Utilizing Database Hooks Like a Pro in Node.js
  • My 7 Must-Have Tools for JavaScript Pros That I Can’t Live Without in 2023
  • A Beginner's Guide to Back-End Development
  • DZone Community Awards 2022

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!