SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Table sort when building table in DOM

    I've been trying just about every sort function I could google but they do not work becuase I am building the table I want to sort dynamically from XML - then appending it to the page using innerHTML. No HTML gets written to the page, hence there are no HTML values to iterate through to sort. Does anyone know a way to capture values that have been extracted from the XML file and then sort them based upon the users selection?

    Thanks for any help

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could have an intermediate data layer between the XML and the HTML table. It could be a 2D javascript array. Then create the string for the innerHTML from the array. Write a function to sort the array depending on which column is chosen.
    Code:
    var dataTable = [["r1c1", "r1c2", "r1c3"], ["r2c1", "r2c2", r2c3"]];
    var sortColumn = 2; // for example
    dataTable.sort(function(a, b){return a[sortColumn] < b[sortColumn] ? 1 : -1;});


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
  •