|
Sortable Table
[Toolbox] [Example] [Source] [Documentation] |
<SCRIPT LANGUAGE="JAVASCRIPT" SRC="sorttable.js"></SCRIPT>Due to a Netscape browser bug, if the page you are using this on is inside a frameset, you may need to include an empty SCRIPT tag before the included file.
<SCRIPT></SCRIPT>Insert the style sheet types in your HTML source, inside <HEAD></HEAD>
<SCRIPT LANGUAGE="JAVASCRIPT" SRC="sorttable.js"></SCRIPT>
<STYLE type=text/css>Define the table somewhere in your HTML file. Preferrably this will go in the <HEAD> of your document, but it may also be included in-line.
.rel {POSITION: relative; width:100%;}
.abs {POSITION: absolute; width:100%;}
.right { text-align:right; }
.left { text-align:left; }
.center { text-align:center; }
</STYLE>
var t = new SortTable("t");This creates a new SortTable object. You must pass the function an argument of the name of the object you are creating. For example...
var mytable = new SortTable("mytable");Now define the columns of your table.
t.AddColumn("Name","","left","");The first argument is the name of the column, which you can use when sorting. The second argument is the <TD> properties. Anything in this argument will be added to the <TD> for each element in this column.
t.AddColumn("ID","","center","numeric");
t.AddColumn("Bonus","BGCOLOR=ffffcc","right","money");
t.AddLine("John Doe",12345,"$500.00");This adds a row of data to the table.
<a href="whatever">032097</a>Sorting based on this text data does not give the correct results, however. Instead, you want to sort based on just the number itself - 032097. For this, you need to define the sort data separately from the cell contents. To continue with the above example, let's say that "John Doe" is actually a link to his home page. Your AddLine would look like this:
t.AddLine("<a href='http://www.johndoe.com/',12345,"$500.00");To correctly sort the first column by only his name, you would then add this command immediately after the AddLine() command:
t.AddLineSortData("John Doe",'','');When blank entries are left for the sort data, the text data entered with AddLine() is used for the sorting.
t.AddLineProperties(text);For example:
t.AddLineProperties("align=center bgcolor=#F6F6F6");This will center the whole row and give it a light gray background color. This option is mostly useful for giving every other row a different background color to make reading easier.
<table border=1 width=600>The links are to the SortRows() function. This will sort and re-populate the table. It will be most common to call this function when a user clicks a table header, but you could actually call this function to sort from anywhere in the page. The sort function requires the table name and column number (starting with index 0) as arguments.
<tr>
<th><a href="javascript:SortRows(t,0)">Name</a></th>
<th><a href="javascript:SortRows(t,1)">ID</a></th>
<th><a href="javascript:SortRows(t,2)">Bonus</a></th>
</tr>
<SCRIPT>t.WriteRows()</SCRIPT>
</table>