A Table is a powerful way to present information. You can make your table even more powerful by giving users the ability to sort it by different columns. In this article, I give you some simple Java Script code that you can use to make almost any table sortable by different columns. I'll explain the code in detail so that you can use it and modify it for your own requirements.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site
Your Ad Here!
Your ad here for one full year, only $20. Send me your ad copy, up to six lines, I'll post it ASAP. After you inspect your ad online, you can pay through PayPal.
Click here for more information.

Easy Code to Sort a Table by Column

A Table is a powerful way to present information. You can make your table even more powerful by giving users the ability to sort it by different columns. In this article, I give you some simple Java Script code that you can use to make almost any table sortable by different columns. I'll explain the code in detail so that you can use it and modify it for your own requirements.

The first thing we need is an example table. The table below is a list of stocks showing share price, number of shares, and put/call ratio. Don't put too much value in the actual table contents. The numbers are fictitious, and I personally don't even know what a put/call ratio is.

Click on a column title to sort the table based on that coulumn.

Stock Price Shares
Put/Call
Yahoo Inc 23.58 400 4/10
Dell Inc. 25.37 500 4/10
Intel Corp. 21.10 200 5/10
Microsoft Corp 28.44 400 1/10
Sun Microsystems 15.77 500 8/10
Cisco Systems 24.18 250 8/10

I designed the table to have a column that is text strings, a column that is integers, a column that is floats, and a column that is fractions. This should handle almost any kind of data you would want to place in a table.

If you want to use this example code for your own table, I recommend that you first try it with the example table. The html code for the example table is shown below. Cut and paste this code into the body section of your webpage.

<table id="tbl1" border=1>
<tr>
  <td onclick="sortColunm(1)">Stock</td>
  <td onclick="sortColunm(2)">Price</td>
  <td onclick="sortColunm(3)">Shares</tr>
  <td onclick="sortColunm(4)">Put/Call</td>
</tr>
<tr>
  <td>Yahoo Inc</td>
  <td>23.58</td>
  <td>400</td>
  <td>4/10</td>
</tr>
<tr>
  <td>Dell Inc.</td>
  <td>25.37</td>
  <td>500</td>
  <td>4/10</td>
</tr>
<tr>
  <td>Intel Corp.</td>
  <td>21.10</td>
  <td>200</td>
  <td>5/10</td>
</tr>
<tr>
  <td>Microsoft Corp</td>
  <td>28.44</td>
  <td>400</td>
  <td>1/10</td>
</tr>
<tr>
  <td>Sun Microsystems</td>
  <td>15.77</td>
  <td>500</td>
  <td>8/10</td>
</tr>
<tr>
  <td>Cisco Systems</td>
  <td>24.18</td>
  <td>250</td>
  <td>8/10</td>
</tr>
</table>

Note that I gave the table an Id of tbl1. This allows us to use the documents getElementById method to get access to the cells within the table. Note that I did not use a table header section. I've always felt using different tags for the cells in the first row of the table was unnecessary. The example code can be easily modified to work with a table header, but it might be easier to just change the header cells to regular table tags.

Web Design Sections

RSS Feed RSS Feed

Easy Java Script Code
Easy Java Script Timer Code
Easy Java Script Windows
Easy Rollovers
Easy Picture Transition Effects
Easy Moving Popup Code
Easy Slide Show Code
Easy Slide Show Code With Linked Slides
Slide Show with Different Size Images
Easy Picture Zoom Code
Easy Picture Panning Code
Easy Java Script Animation
Easy JavaScript Picture Selector Code
Easy Animated Banners with Java Script
Easy Java Script Form Validation
Easy Drag n Drop Code
Easy Graph Generating Code
Easy Code to Sort a Table by Column
Web Designer's Reference
Easy Fading Text Banner Code
Easy Expanding Banner Code
Calendars for Your Website
Date Picker For Your Web Site
Calculators For Your Web Site
Loan Payment Calculator
Length Units Converter
Body Mass Index
Fahrenheit to Celsius Converter
Decimal to Hexidecimal Converter
Easy Code for Screen Tape Adding Machine
Code for Java Script Circle⁄Sphere Calculator
Round a Float to 4 Digits to the Right of the Decimal Point
Make Your Own Graphical Digital Clock
Let Your Web site Visitors Set Font Size
How to Disable the Browser Back Button
Add More Bang to Your Content With Keyword Popup Menus
Put a Music Off Switch on Your Webpage
Java Script Random Password Generator
Password Protection Using Java Script
Replace Drop-down with Drag-and-drop
Submit Forms Without CGI
Code for a Less annoying Popup Window
Using the Java Script Array Object
Using the Java Script Date Object
Java Script Message Boxes
How to Shuffle the Deck With Java Script
Web Site Menus : Which Section Am I In?
How Far Did the User Scroll?
Where Did the User Click?
Four Ways to Use Java Script Event Handlers
Create Your Own Database Using Only Notepad : CDV

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269