Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 18th, 2003, 05:45 AM
Authorized User
 
Join Date: Jun 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to vickriz
Default Table with data

Hello folks i found this JavaScript code which sort table. its working on their demo page. but when i use it on my own and click the table header it occurs "error on page" at my window.status
anybody want to help??:)
here's the whole code..
Code:
JavaScript:
<!-- Begin Demo: Sorting data from table
function setdataType(cValue)
  {
    // THIS FUNCTION CONVERTS DATES AND NUMBERS FOR PROPER ARRAY
    // SORTING WHEN IN THE SORT FUNCTION
    var isDate = new Date(cValue);
    if (isDate == "NaN")
      {
        if (isNaN(cValue))
          {
            // THE VALUE IS A StrING, MAKE ALL CHARACTERS IN
            // StrING UPPER CASE TO ASSURE PROPER A-Z SORT
            cValue = cValue.toUpperCase();
            return cValue;
          }
        else
          {
            // VALUE IS A NUMBER, TO PREVENT StrING SORTING OF A NUMBER
            // ADD AN ADDITIONAL DIGIT THAT IS THE + TO THE LENGTH OF
            // THE NUMBER WHEN IT IS A StrING
            var myNum;
            myNum = String.fromCharCode(48 + cValue.length) + cValue;
            return myNum;
          }
        }
  else
      {
        // VALUE TO SORT IS A DATE, REMOVE ALL OF THE PUNCTUATION AND
        // AND RETURN THE StrING NUMBER
        //BUG - StrING AND NOT NUMERICAL SORT .....
        // ( 1 - 10 - 11 - 2 - 3 - 4 - 41 - 5  etc.)
        var myDate = new String();
        myDate = isDate.getFullYear() + " " ;
        myDate = myDate + isDate.getMonth() + " ";
        myDate = myDate + isDate.getdate(); + " ";
        myDate = myDate + isDate.getHours(); + " ";
        myDate = myDate + isDate.getMinutes(); + " ";
        myDate = myDate + isDate.getSeconds();
        //myDate = String.fromCharCode(48 + myDate.length) + myDate;
        return myDate ;
      }
  }
function sorttable(col, tableToSort)
  {
    var iCurCell = col + tableToSort.cols;
    var totalRows = tableToSort.rows.length;
    var bSort = 0;
    var colArray = new Array();
    var oldIndex = new Array();
    var indexArray = new Array();
    var bArray = new Array();
    var newRow;
    var newCell;
    var i;
    var c;
    var j;
    // ** POPULATE THE ARRAY colArray WITH CONTENTS OF THE COLUMN SELECTED
    for (i=1; i < tableToSort.rows.length; i++)
      {
        colArray[i - 1] = setdataType(tableToSort.cells(iCurCell).innerText);
        iCurCell = iCurCell + tableToSort.cols;
      }
    // ** COPY ARRAY FOR COMPARISON AFTER SORT
    for (i=0; i < colArray.length; i++)
      {
        bArray[i] = colArray[i];
      }
    // ** SORT THE COLUMN ITEMS
    //alert ( colArray );
    colArray.sort();
    //alert ( colArray );
    for (i=0; i < colArray.length; i++)
      { // LOOP THROUGH THE NEW SORTED ARRAY
        indexArray[i] = (i+1);
        for(j=0; j < bArray.length; j++)
          { // LOOP THROUGH THE OLD ARRAY
            if (colArray[i] == bArray[j])
              {  // WHEN THE ITEM IN THE OLD AND NEW MATCH, PLACE THE
                // CURRENT ROW NUMBER IN THE PROPER POSITION IN THE
                // NEW ORDER ARRAY SO ROWS CAN BE MOVED ....
                // MAKE SURE CURRENT ROW NUMBER IS NOT ALREADY IN THE
                // NEW ORDER ARRAY
                for (c=0; c<i; c++)
                  {
                    if ( oldIndex[c] == (j+1) )
                    {
                      bSort = 1;
                    }
                      }
                      if (bSort == 0)
                        {
                          oldIndex[i] = (j+1);
                        }
                          bSort = 0;
                        }
          }
    }
  // ** SORTING COMPLETE, ADD NEW ROWS TO BASE OF table ....
  for (i=0; i<oldIndex.length; i++)
    {
      newRow = tableToSort.insertrow();
      for (c=0; c<tableToSort.cols; c++)
        {
          newCell = newRow.insertCell();
          newCell.innerHTML = tableToSort.rows(oldIndex[i]).cells(c).innerHTML;
        }
      }
  //MOVE NEW ROWS TO TOP OF table ....
  for (i=1; i<totalRows; i++)
    {
      tableToSort.moveRow((tableToSort.rows.length -1),1);
    }
  //DELETE THE OLD ROWS FROM THE BOTTOM OF THE table ....
  for (i=1; i<totalRows; i++)
    {
      tableToSort.deleteRow();
    }
  }
//  End -->

HTML:

<table width="75%" border=1 cellspacing="1" cellpadding="1" name="rstable" id=rstable  cols=5>
    <tr bgcolor=mediumblue>
        <td><a href="javascript:sorttable(0, rstable);"><b>ID</b></a></td>
        <td><a href="javascript:sorttable(1, rstable);"><b>NAME</b></a></td>
        <td><a href="javascript:sorttable(2, rstable);"><b>DATE</b></a></td>
        <td><a href="javascript:sorttable(3, rstable);"><b>PHONE</b></a></td>
        <td><a href="javascript:sorttable(4, rstable);"><b>WORKFLOW</b></a></td>
    </tr>
    <tr>
        <td>1</td>    <td>Andy Berry</td>    <td>4/9/72</td>    <td>763-555-1212</td> <td>REVIEW</td>
    </tr>
    <tr>
        <td>2</td>    <td>Dan Developer</td>    <td>9/3/63</td>    <td>215-555-1400</td> <td>SAME</td>
    </tr>
    <tr>
        <td>3</td>    <td>John Javascript</td> <td>3/4/71</td> <td>612-555-0987</td> <td>REVIEW</td>
    </tr>
    <tr>
        <td>4</td> <td>Jerry JSPage</td> <td>8/2/71</td> <td>215-555-7654</td> <td>SAME</td>
    </tr>
    <tr>
        <td>5</td> <td>Mary Mainframe</td> <td>3/28/70</td> <td>763-555-8295</td> <td>REVIEW</td>
    </tr>
    <tr>
        <td>6</td> <td>Elaine Ecommerce</td> <td>2/28/29</td> <td>612-555-1338</td> <td>REVIEW</td>
    </tr>
    <tr>
        <td>7</td> <td>John Smith</td> <td>12/31/00</td> <td>610-555-0987</td> <td>SAME</td>
    </tr>
    <tr>
        <td>8</td> <td>Candy Coder</td> <td>4/1/70</td> <td>000-555-9099</td> <td>SAME</td>
    </tr>
        <tr>
        <td>9</td> <td>Pippy Long Stocking</td> <td>1/1/30</td> <td>613-555-1338</td> <td>DIFFERENT</td>
    </tr>
    <tr>
        <td>10</td> <td>111222</td> <td>2/2/01</td> <td>345-555-7654</td> <td>DIFFERENT</td>
    </tr>
    <tr>
        <td>11</td> <td>Apple Man</td> <td>3/13/74</td> <td>215-555-4043</td> <td>DIFFERENT</td>
    </tr>
</table>
Mabuhay!!!
__________________
Mabuhay!!!





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to delete all data from table TomW Access VBA 5 March 12th, 2008 09:11 AM
Updating one table with data from another table dirtdog22 Access VBA 1 January 21st, 2008 04:41 PM
Get data From Table susman Classic ASP Professional 0 October 25th, 2007 06:14 AM
How to Update one table with other table data? ramk_1978 SQL Language 2 May 26th, 2006 12:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.