Table Sort Example 4 : Table Sorter « GUI Components « JavaScript Tutorial

JavaScript Tutorial
1. Language Basics
2. Operators
3. Statement
4. Development
5. Number Data Type
6. String
7. Function
8. Global
9. Math
10. Form
11. Array
12. Date
13. Dialogs
14. Document
15. Event
16. Location
17. Navigator
18. Screen
19. Window
20. History
21. HTML Tags
22. Style
23. DOM Node
24. Drag Drop
25. Object Oriented
26. Regular Expressions
27. XML
28. GUI Components
29. Animation
30. MS JScript
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
JavaScript Tutorial » GUI Components » Table Sorter 
28. 3. 4. Table Sort Example 4
<!--
The following code is from 

Professional JavaScript for Web Developers
by Nicholas C. Zakas

ISBN: 978-0-7645-7908-0
April 2005

http://www.nczonline.net/
For purchase: http://www.amazon.com/Professional-JavaScript-Developers-Wrox-Guides/dp/0764579088
For publisher website: http://www.wrox.com/WileyCDA/WroxTitle/productCd-0764579088.html
-->
<html>
    <head>
  <title>Table Sort Example</title>
        <script type="text/javascript">
        
            function convert(sValue, sDataType) {
                switch(sDataType) {
                    case "int":
                        return parseInt(sValue);
                    case "float":
                        return parseFloat(sValue);
                    case "date":
                        return new Date(Date.parse(sValue));
                    default:
                        return sValue.toString();
                
                }
            }
        
            function generateCompareTRs(iCol, sDataType) {
        
                return  function compareTRs(oTR1, oTR2) {
                            var vValue1, vValue2;
        
                            if (oTR1.cells[iCol].getAttribute("value")) {
                                vValue1 = convert(oTR1.cells[iCol].getAttribute("value"),
                                              sDataType);
                                vValue2 = convert(oTR2.cells[iCol].getAttribute("value"),
                                              sDataType);
                            else {
                                vValue1 = convert(oTR1.cells[iCol].firstChild.nodeValue,
                                              sDataType);
                                vValue2 = convert(oTR2.cells[iCol].firstChild.nodeValue,
                                              sDataType);
                            }
        
                            if (vValue1 < vValue2) {
                                return -1;
                            else if (vValue1 > vValue2) {
                                return 1;
                            else {
                                return 0;
                            }
                        };
            }
           
            function sortTable(sTableID, iCol, sDataType) {
                var oTable = document.getElementById(sTableID);
                var oTBody = oTable.tBodies[0];
                var colDataRows = oTBody.rows;
                var aTRs = new Array;
        
                for (var i=0; i < colDataRows.length; i++) {
                    aTRs[i= colDataRows[i];
                }
        
                if (oTable.sortCol == iCol) {
                    aTRs.reverse();
                else {
                    aTRs.sort(generateCompareTRs(iCol, sDataType));
                }
        
                var oFragment = document.createDocumentFragment();
                for (var i=0; i < aTRs.length; i++) {
                    oFragment.appendChild(aTRs[i]);
                }
       
                oTBody.appendChild(oFragment);
                oTable.sortCol = iCol;
            }

        </script>
    </head>
    <body>
        <P>Click on the table header to sort.</p>
        <table border="1" id="tblSort">
            <thead>
                <tr>
                    <th onclick="sortTable('tblSort', 0)" style="cursor:pointer">Type</th>
                    <th onclick="sortTable('tblSort', 1)" style="cursor:pointer">Filename</th>                    
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td value="doc"><img src="images/wordicon.gif"/></td>
                    <td>My Resume.doc</td>
                </tr>
                <tr>
                    <td value="xls"><img src="images/excelicon.gif"/></td>
                    <td>Fall Budget.xls</td>
                </tr>
                <tr>
                    <td value="pdf"><img src="images/acrobaticon.gif"/></td>
                    <td>How to be a better programmer.pdf</td>
                </tr>
                <tr>
                    <td value="doc"><img src="images/wordicon.gif"/></td>
                    <td>My Old Resume.doc</td>
                </tr>
                <tr>
                    <td value="txt"><img src="images/notepadicon.gif"/></td>
                    <td>Notes from Meeting.txt</td>
                </tr>
                <tr>
                    <td value="zip"><img src="images/zippedfoldericon.gif"/></td>
                    <td>Backups.zip</td>
                </tr>
                <tr>
                    <td value="xls"><img src="images/excelicon.gif"/></td>
                    <td>Spring Budget.xls</td>
                </tr>
                <tr>
                    <td value="doc"><img src="images/wordicon.gif"/></td>
                    <td>Job Description - Web Designer.doc</td>
                </tr>
                <tr>
                    <td value="pdf"><img src="images/acrobaticon.gif"/></td>
                    <td>Saved Web Page.pdf</td>
                </tr>
                <tr>
                    <td value="doc"><img src="images/wordicon.gif"/></td>
                    <td>Chapter 1.doc</td>
                </tr>
            </tbody>
        </table>      
    </body>
</html>
28. 3. Table Sorter
28. 3. 1. Table Sort Example
28. 3. 2. Table Sort Example 2
28. 3. 3. Table Sort Example 3
28. 3. 4. Table Sort Example 4
28. 3. 5. Table Sort Example 5
28. 3. 6. System Drag And Drop Example
28. 3. 7. Assign mouse move event coordinate to element style
ww_w_.j__a_v__a___2__s_._com | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.