Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7997137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:53:56+00:00 2026-06-04T14:53:56+00:00

I’m building a table sorter and pagination script. Sorting works fine, and pagination on

  • 0

I’m building a table sorter and pagination script. Sorting works fine, and pagination on the last table works as a charme as well. The pagination’s previous and next buttons are broken on all previous tables. Can somebody tell me why?

Here is the jquery plugin:

 (function($)
{
    $.fn.tableSorterTwo = function(options)
    {
        var defaults = {
            pagination: 20
        }

        var options = $.extend(defaults, options);

        return this.each(function()
        {
            $(this).find('thead').addClass('noselect');
            $(this).find('thead th').each(function(column)
            {  
                $(this).addClass('sorting').click(function()
                {  
                    var $table = $(this).parent().parent().parent();
                    var findSortKey = function($cell)
                    {  
                        return $cell.find('.sort-key').text().toUpperCase() + ' ' + $cell.text().toUpperCase();  
                    }; 

                    var sortDirection = $(this).is('.sorting_asc') ? -1 : 1;  
                    var $rows = $table.find('tbody tr').get();  

                    $.each($rows, function(index, row)
                    {  
                        row.sortKey = findSortKey($(row).children('td').eq(column));  
                    });  

                    $rows.sort(function(a, b)
                    {  
                        if(!isNaN(a.sortKey) && !isNaN(b.sortKey))
                        {
                            var val = a.sortKey-b.sortKey;
                            if(val < 0) return -sortDirection;
                            if(val > 0) return sortDirection;                       
                        }
                        else if(a.sortKey.indexOf("%") >= 0)
                        {
                            var a = a.sortKey.replace(' %', '');
                            var b = b.sortKey.replace(' %', '');
                            var val = a-b;
                            if(val < 0) return -sortDirection;
                            if(val > 0) return sortDirection;
                        }
                        else
                        {
                            if(a.sortKey < b.sortKey) return -sortDirection;  
                            if(a.sortKey > b.sortKey) return sortDirection;  
                        }
                        return 0;  
                    });  

                    $.each($rows, function(index, row)
                    {  
                        $table.find('tbody').append(row);  
                        row.sortKey = null;  
                    });  

                    $table.find('thead th').removeClass('sorting_asc sorting_desc');  
                    var $sortHead = $table.find('thead th').filter(':nth-child(' + (column + 1) + ')');  
                    sortDirection == 1 ? $sortHead.addClass('sorting_asc') : $sortHead.addClass('sorting_desc');  
                    $table.find('tbody tr').removeClass('sorting').filter(':nth-child(' + (column + 1) + ')').addClass('sorting'); 
                    zebraRows($table);
                });     
            });

            // PAGINATION
            var totalRows = $(this).find('tbody tr').size();
            if(totalRows > options.pagination)
            {
                var totalPages = Math.ceil(totalRows/options.pagination);
                $(this).find('tbody').after('<tfoot><tr><th colspan="3" class="table-pagination"></th></tr></tfoot>');
            }

            var currentLink = 0;
            var numberLinks = ''; 
            if(totalPages > 1)
            {
                while(totalPages > currentLink)
                {
                    var active = (currentLink == 0) ? ' table-pagination-link-active' : '';
                    checkLinks($(this), currentLink, totalPages);
                    numberLinks += '<a href="' + (currentLink+1) + '" class="table-pagination-link' + active + '">' + (currentLink+1) + '</a>';
                    currentLink++;
                }
                $(this).find('tfoot tr th').html('<a href="1" class="table-pagination-link table-pagination-link-first no-active">&#171;</a>' +
                '<a href="1" class="table-pagination-link table-pagination-link-previous no-active">&#8249;</a>' +
                numberLinks +
                '<a href="2" class="table-pagination-link table-pagination-link-next no-active">&#8250;</a>' +
                '<a href="' + totalPages + '" class="table-pagination-link table-pagination-link-last no-active">&#187;</a> | <span class="table-pagination-overview no-active">1</span>/<span class="table-pagination-total no-active">' + totalPages + '</span>');
                $(this).find('tbody tr').hide().slice(0, options.pagination).show();
                $(this).find('.table-pagination-link-previous').hide();
                $(this).find('.table-pagination-link-first').hide();
            }

            $('.table-pagination-link').click(function()
            {
                var table = $(this).parents('table');
                var href = $(this).attr('href');
                $(table).find('.table-pagination-link').removeClass('table-pagination-link-active');
                if(!$(this).hasClass('no-active'))
                {
                    $(this).addClass('table-pagination-link-active');
                }
                if(href == totalPages)
                {
                    markActive(table, totalPages)
                }
                else if(href == 1)
                {
                    markActive(table, href);
                }
                goToPage(table, href, options.pagination);
                return false;
            });
        });
    };

    function zebraRows(table)
    {
        $(table).find('tbody tr:even').removeClass('even').addClass('even');
    }

    function markActive(table, href)
    {
        $(table).find('a[href="' + href + '"]').addClass('table-pagination-link-active');
        $(table).find('.no-active').removeClass('table-pagination-link-active');
    }   

    function goToPage(table, page, pagination)
    {
        var previous = parseInt(page)-1;
        var next = parseInt(page)+1;
        $(table).find('.table-pagination-overview').html(page);
        var page = parseInt(page)-1;
        var start = page*pagination;
        var end = start+pagination;
        $(table).find('tbody tr').hide().slice(start, end).show();
        checkLinks(table, page, $(table).find('.table-pagination-total').html());
        $(table).find('.table-pagination-link-previous').attr('href', previous);
        $(table).find('.table-pagination-link-next').attr('href', next);
        return false;
    }

    function checkLinks(table, currentLink, totalPages)
    {
        if(currentLink == 0)
        {
            $(table).find('.table-pagination-link-previous').hide();
            $(table).find('.table-pagination-link-first').hide();
            $(table).find('.table-pagination-link-next').show();
            $(table).find('.table-pagination-link-last').show();
        }
        else if(currentLink == (totalPages-1))
        {
            $(table).find('.table-pagination-link-next').hide();
            $(table).find('.table-pagination-link-last').hide();
            $(table).find('.table-pagination-link-previous').show();
            $(table).find('.table-pagination-link-first').show();
        }
        else
        {
            $(table).find('.table-pagination-link-previous').show();
            $(table).find('.table-pagination-link-first').show();
            $(table).find('.table-pagination-link-next').show();
            $(table).find('.table-pagination-link-last').show();
        }
    }
})(jQuery);

Call function: $('.data-table').tableSorterTwo({pagination: 15})

I also have an jsfiddle in which I’m working: http://jsfiddle.net/Tra9N/15/

Not the purpose of this question, but if anybody has improvements or tips for me about the plugin, please share them. It is still not finished and my first jquery plugin so any tips are appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:53:57+00:00Added an answer on June 4, 2026 at 2:53 pm

    In your jsfiddle example the change page event is firing twice for each click, which is causing the back button to skip back two pages instead of one.

    It looks like this is happening because you have two tables in your example and your code wires up the click event to every .table-pagination-link link in each loop. You can fix this by limiting your scope to the current table. So replace:

    $('.table-pagination-link').click(function()
    

    With this:

    $('.table-pagination-link', this).click(function()
    

    Working jsFiddle example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.