ascii image


0010101000011111101001101010000010011000
1110101110110101011011111010010010001011
0001011100011101111001010011010010111110
0000010111101001100000110011101100001000
0011000000111010001111111000100110000001
1010110110000000000001011001000110001010
0101100010101100000100000010100100010101
0001011001011101100011000101110111101110
0110010100110100111101110100110011111101
0010111100110011010010110010101111011011
0100000000001001001011000010110100101001
1101000111100000110111011100110111000010
1111110001111111101101001010000111101100
0010110000100000111011000000101100010110
0101111000011100111010000000011111101111
0010010011110010011101001000110101000101
0000000001000100001111111100111010001111

Tuesday, 23 August 2011

Sort HTML table with a row header (2 row sort)

Table
ID NAME Value SPECIALIZATION
Row Header 11
1Angelina-1Computer Science
Row Header 22
2Martina10Mathematics
Row Header 33
3Tina0.5English
Row Header 44
4Simran100Biology
Row Header 55
5Christina-99Psychology
The code below allows the above table to sort the data values while keeping the row headers above. Something i couldn't find an example for on Google.
 function doSort(column, tableID, direction) {  
   var data = []  
   var table = document.getElementById(tableID);  
   var skip = 1; // skip column headers  
   // set up sortable data array [ sorted key, row data, header data ]  
   for (var i = skip + 1, l = table.rows.length; i < l; i=i+2) {  
           // grab each row - so we can clone later  
     var header_cell_row = table.rows[i - 1]; // header cell row data  
     var data_row = table.rows[i]; // sortable row data  
            var cell_data = table.rows[i].cells[column].innerHTML; // this is the bit we sort.  
             data.push([parseFloat(cell_data), data_row, header_cell_row])  
   };  
   //sort that array in the direction we ask  
   data.sort(  
     direction==0 ?  
       function(a,b){return a[0]<b[0]?-1:a[0]>b[0]?1:0} // ASC  
     :  
       function (a, b) { return a[0] > b[0] ? -1 : a[0] < b[0] ? 1 : 0} // DESC  
   )  
   // run thru sorted array  
   for (var i = 0, l = data.length; i < l; i++) {  
           // we append at the bottom of the original table the new sorted table  
           // as we have saved the real rows we can clone and append  
     //  
     // header row  
     //  
     var source = data[i][2];  
            table.appendChild(source.cloneNode(true));  
     //  
     // data row  
     //  
     var source = data[i][1];  
            table.appendChild(source.cloneNode(true));  
           //  
           // delete an old row from the table  
           //  
          table.deleteRow(1);  
           table.deleteRow(1);  
   }  
 }  

No comments:

Post a comment