Accepted answer

Glad you came back with what you've tried until now based off the comment I posted. I couldn't work on it yesterday as I got too busy with work. Anyway, got it working now.

I made a few changes to it and also added certain additional features (First, Prev, Next, Last) and seems to be working okay now.

HTML added:

<div id="toggle-buttons">
<button class="button" data-id="first">
          First Page
<button class="button disabled" data-id="previous">
          Prev Page
<button class="button" data-id="next">
          Next Page
<button class="button" data-id="last">
          Last Page
<button class="button" id="all-button">
          Show All

Javascript to paginate the table based on the button click:

const totalBodies = $('tbody').length;
var counter = 0; // set counter initially to 0

d3.selectAll('#toggle-buttons button').on('click', function () {

    return ;

switch('data-id')) {
    case 'first':
        counter = 0;'[data-id="previous"]').classed('disabled', true);'[data-id="next"]').classed('disabled', false);
  case 'last':
        counter = totalBodies-1;'[data-id="next"]').classed('disabled', true);   '[data-id="previous"]').classed('disabled', false);        
  case 'next':
    if(counter === totalBodies-1) {'disabled', true);
    }'[data-id="previous"]').classed('disabled', false);         
  case 'previous':
    if(!counter) {'disabled', true);
    }'[data-id="next"]').classed('disabled', false);         

Explanation (pretty much understandable I suppose):

  1. Switch case to determine the button click based on which the counter will change. Important Toggling the disabled class on the buttons.
  2. redraw(counter) on every button click (unless it has a disabled class)

Here's a fork of your fiddle:

I'm selecting the first page by default. You can change that according to the requirement and add the disabled class in the initial phase accordingly. Also, I see a table nested within a table which doesn't seem right - maybe tablesorter adds a table tag as well. You can take a look at that.

Hope this helps.


I got some of it myself based off what Shashank posted:

        let tbodiesPerPage = 10;
    const totalBodies = $('tbody').length;"#buttons").datum({
        portion: 0

    // the chain select here pushes the datum onto the up and down buttons also"#buttons").select("#previous-button").on("click", function (d) {
        console.log('next was ', d.portion);
        if (d.portion - tbodiesPerPage >= 0) {
            d.portion -= tbodiesPerPage;
    });"#buttons").select("#next-button").on("click", function (d) {
        // let the bodies hit the floor
        console.log('previous was', d.portion);
        if (d.portion < (totalBodies - tbodiesPerPage)) {
            d.portion += tbodiesPerPage;

    function redraw(start) {
            .style("display", function (d, i) {
                return i >= start && i < start + tbodiesPerPage ? null : "none";


Related Query

More Query from same tag