SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Sorting Tables

  1. #1
    SitePoint Member
    Join Date
    Feb 2003
    Location
    Augusta, GA
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sorting Tables

    I'm using php and mysql to do a query and draw up a table from the results of that query. I want people to be able to sort the table by different columns.

    I can do this using php and mysql to requery and rewrite the entire page. That works, but it requires the server to work and resend the identical query set of data that is just sorted differently.

    I see in some web searches that Java can do this. It makes sense that the client computer can sort the dataset and repost it without having the server to requery, sort, and resend it. But I can't find a tutorial to teach me.

    1) Can Java or Javascript do this?
    2) Anyone got some recommendations of tutorials that I can work through to figure out how to implement it?

    Thanks

  2. #2
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heres an example
    Attached Files Attached Files

  3. #3
    Jamison DaDaHost's Avatar
    Join Date
    Sep 2002
    Location
    Florida, USA
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a fantastic set of IE behaviours you can use. I have used them several times in web applications where we could control the browser type and version.

    MSDN Dave Massey's Table Behaviors

    Jamie

  4. #4
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a note, databinding will only work in internet explorer

  5. #5
    SitePoint Member
    Join Date
    Feb 2003
    Location
    Augusta, GA
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, so this is an XML/XSL thing, not a Java or Javascript thing?

  6. #6
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can do a number of things such as remote scripting or using xmlhttp object in javascript and rewrite a div tag with the response etc...

  7. #7
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found a simple but powerful example a while ago:
    http://www.romankoch.ch/capslock/tablesort.htm

    However, it becomes a little more complex if you have html in the cells because using innerHTML instead of innerText dramatically slows things down.

    I was wondering if anybody could help me tune my sorting script. I've taken the one from the link above and modified it a bit for my needs. There are three main components to the script: create array from table, sort the array, and write the array back to the table. I've been profiling the script so far and the bulk of the time is being spent in the writing back to the table. Here's what I have for this part:

    Code:
    for (i=0; i < myRows; i++) {
        for (j=0; j < myCols; j++) {
            mySource.rows[i].cells[j].innerHTML = myArray[i][j][0]
            mySource.rows[i].cells[j].className = myArray[i][j][1]
        }
    }
    For 200 rows, this can take almost 27 seconds, so as you can see, I've got some problems. I tried to do this instead:

    Code:
    var tableHTML = ""
    for (i=0; i < myRows; i++) {
        var rowHTML = "<tr>";
        for (j=0; j < myCols; j++) {
            rowHTML += "<td class=\""+myArray[i][j][1]+"\">";
            rowHTML += myArray[i][j][0];
            rowHTML += "</td>";
        }
        rowHTML += "</tr>";
        tableHTML += rowHTML;
    }
    mySource.innerHTML = tableHTML;
    But, it doesn't work in IE because table, tbody, and tr are all readonly. It works great in Mozilla. I've looked at the Microsoft reference for building tables dynamically, and since you have to add each row and cell manually, it looks to be slower that what I've got.

    Any ideas?

    Thanks,
    Ryan
    Last edited by willie35; Jul 22, 2003 at 16:27.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •