Jquery Table Sorter

This is a simple Jquery table sorter click the table headers to sort

Hope this helps you, please leave comments below on how to improve this or just to say Hi

Result

ID Name Age Job
1 John 25 Pilot
2 Mark 43 Joiner
3 Jane 22 Designer
 <!DOCTYPE html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Lightbox Demo</title>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <style type="text/css">
  td, th {
  border: 1px solid #111;
  padding: 6px;
  }
  th {
  font-weight: 700;
  }
  </style>
  <script type='text/javascript'>//<![CDATA[ 
  $(window).load(function(){
  var sortTable = $('table');
  $('#sortId, #sortName, #sortAge, #sortJob')
  .each(function(){
  var th = $(this),
  thIndex = th.index(),
  inverse = false;
  th.click(function(){
  sortTable.find('td').filter(function(){
  return $(this).index() === thIndex;
  }).sortElements(function(a, b){
  return $.text([a]) > $.text([b]) ?
  inverse ? -1 : 1
  : inverse ? 1 : -1;
  }, function(){
  return this.parentNode; 
  });
  inverse = !inverse;
  });
  });
  });//]]>
  </script>
  </head>
  <body>
  <table>
  <tr>
  <th id="sortId">ID</th>
  <th id="sortName">Name</th>
  <th id="sortAge">Age</th>
  <th id="sortJob">Job</th>
  </tr>
  <tr>
  <td>1</td>
  <td>John</td>
  <td>25</td>
  <td>Pilot</td>
  </tr>
  <tr>
  <td>2</td>
  <td>Mark</td>
  <td>43</td>
  <td>Joiner</td>
  </tr>
  <tr>
  <td>3</td>
  <td>Jane</td>
  <td>22</td>
  <td>Designer</td>
  </tr>
  </table>
  <script type="text/javascript">
  jQuery.fn.sortElements = (function(){
  var sort = [].sort;
  return function(comparator, getSortable) {
  getSortable = getSortable || function(){return this;};
  var placements = this.map(function(){
  var sortElement = getSortable.call(this),
  parentNode = sortElement.parentNode,
  nextSibling = parentNode.insertBefore(
  document.createTextNode(''),
  sortElement.nextSibling
  );
  return function() {
  parentNode.insertBefore(this, nextSibling);
  parentNode.removeChild(nextSibling);
  }; 
  }); 
  return sort.call(this, comparator).each(function(i){
  placements[i].call(getSortable.call(this));
  }); 
  }; 
  })();
  </script>
  </body>
  </html>

Available For Freelance:

I Am Available To Work For You Freelance, See My Skills To See If I Can Help Your Project, If I Can Help You Then Please Contact Me.

My Rates Start From £180 A Day, Discount Prices For Regular Clients And Work Over 3 Days.

I Started To Learn Web Development In 2008, I Did A Few Open University Courses While Working Full Time In The Building Trade, I Managed To Get A Diploma With Distinction

Request A Tutorial

If You Are Having Difficulty With The Development Of Your Website Or If You Are Just Starting Out Leaning Web Development And You Think I Can Help You Then Request A Tutorial, I Will Do My Best To Help You

I Dont Charge For My Services If I Can Help You With Something That Can Be Be Fixed In A Few Minutes.

Advertise

If You Would Like To Advertise On This Site Then Please Contact Me, You Will Be Given Login Details To See What Day And Where Clicks Have Come From.

Prices Start From £10 A Month With Discounts Available For 3 Months Or More.

You Can Either Send Me The Banner Advert That You Have Designed Or From £10 I Can Have Designer Design A Banner For You.

Please Leave A Comment And A Rating





image

Chris Gave This Code Rating

This is a simple Jquery table sorter, click in the table headers to sort the order please leave feedback and rate

www.codecaven.co.uk image 5.00 out of 5 based on 1 ratings. 1 user reviews.

My Skills:

  • PHP 5
  • JavaScript
  • Jquery
  • XHTML Validation
  • KineticJS
  • HTML5
  • CSS - CSS3
  • SEO

My Social Networks:

  • facebook youtube