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 7595823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:46:00+00:00 2026-05-30T21:46:00+00:00

I have following data table , with individual sorting ascending and descending buttons. I

  • 0

I have following data table , with individual sorting ascending and descending buttons.
I am using jQuery plugin “jQuery.fn.sort” James Padolsey

Here is the working example

http://jsbin.com/alawub/2/edit

enter image description here

I want to add sorting to each Col. but its not working please review my JS code above any other alternative solution for this is welcome.

  • 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-05-30T21:46:01+00:00Added an answer on May 30, 2026 at 9:46 pm

    You are adding the click handlers too many times:

    $('th')
        .wrapInner('<span title="sort this column"/>')
        .each(function(){
            ...
            $('#accending_1,#accending_2').click(function(e){
    

    What this will do is for every th, and there are two rows of 4 th tags, add a click handler to the elements with id accending_1 and accending_2. That will add 8 click handlers to each button!

    There are numerous ways to fix this. Instead of having specific id’s for each button use classes and find them relative to the header:

    $('th')
        .wrapInner('<span title="sort this column"/>')
        .each(function(){
    
            $('.accending', this).click(
    

    example

    Note the this parameter on the last line that limits the selector to descendants of the current TH. That would still be doing searches for the top row of TH’s though.

    It’s probably better to just search directly for the buttons and then work out what column they are in.

      $('.accending, .accending')
        .wrapInner('<span title="sort this column"/>')
        .click(function(e){
          console.log("click");
          var th = $(this).closest('th'),
            thIndex = th.index();
          table.find('td')
            .filter(function(){
              return $(this).index() === thIndex;
            })
            .sort(
              function(a, b){
                return $.text([a]) > $.text([b]);
              }, function(){
                // parentNode is the element we want to move
                return this.parentNode; 
              }
            );
        });
      $('.decending, .decending')
    

    example

    There’s still a lot of duplication in the code, and html.

    The accending and decending click handlers are almost the same, so lets just pass in the sort function.

      //This function returns another function which is a click handler. 
      //It takes the sort function as a parameter.
      function createClickHandler(sortFunction){
        return function(e){
          var th = $(e.target).closest('th'),
            thIndex = th.index();
          console.log(th);
          table.find('td')
            .filter(function(){
              return $(this).index() === thIndex;
            })
            .sort(sortFunction, function(){
                // parentNode is the element we want to move
                return this.parentNode; 
              }
            );
        };
      }
    
      $('.accending, .accending')
        .wrapInner('<span title="sort this column"/>')
        .click(createClickHandler(function(a, b){
            return $.text([a]) > $.text([b]);
          })
        );
      $('.decending, .decending')
        .wrapInner('<span title="sort this column"/>')
        .click(createClickHandler(function(a, b){
            return $.text([a]) < $.text([b]);
          })
        );
    

    example

    The HTML can also be cleaned up a bit. The tags for the buttons are all the same so lets insert them from javascript adding the click handlers to them directly instead of having to search for them. Since we’re iterating over the column headers again we can clean up how we get the column number. And finally, passing two different sort functions is a bit wasteful so let’s pass a direction parameter instead.

      //This function returns another function which is a click handler. 
      //It takes the sort function as a parameter.
      function createClickHandler(column, isAccending){
        return function(e){
          table.find('td')
            .filter(function(){
              return $(this).index() === column;
            })
            .sort(function(a, b){
              var order = $.text([a]) > $.text([b]);
              return isAccending ? order : !order;
            }, function(){
              // parentNode is the element we want to move
              return this.parentNode; 
            })
          ;
        };
      }
    
      $('#controls th').each(function(column,item){
        //Note we get the column index for for free with the each function
        $(this)
          .append($('<a title="sort this column" href="#">Ascending</a>')
            .click(createClickHandler(column, true))
          )
          .append('&nbsp;&nbsp;')
          .append($('<a title="sort this column" href="#">Decending</a>')
            .click(createClickHandler(column, false))
          )
          ;
      });
    

    example


    Note that I removed the inverse variable. It was never being used.

    return $.text([a]) > $.text([b]) 
        inverse ? -1 : 1
      ;
    

    I’m not sure what you thought this was doing but it’s actually returning on the first line due to automatic semicolon insertion. It will be interpreted as:

    return $.text([a]) > $.text([b]);
        inverse ? -1 : 1;
    

    so the inverse is dead code. It’s one of the javascript’s bad bits and not very obvious. jsbin was warning you about missing semicolons. It’s always worth fixing any errors/warnings before submitting code here.

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

Sidebar

Related Questions

I have following data in my table. alt text http://img26.imageshack.us/img26/3746/productfield.png I want to extract
Hi I have following data in the table: ID-----startDate----endDate 5549 2008-05-01 4712-12-31 5567 2008-04-17
Suppose we have the following table data: ID parent stage submitted 1 1 1
I have a sql table which have the following data, Id City Country ---
I have the following table and data in SQL Server 2005: create table LogEntries
I have a table with the following data: id | numbers | date ----------------------------------
I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`,
Let's say I have a table containing following data: | id | t0 |
I have a table emp with following structure and data: name dept salary -----
Consider the following data model: Suppose I have a table called SuperAwesomeData where each

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.