2016-11-19 48 views
1

對不起降序排列,如果這個問題是問我在JS新之前,請幫助我。這是按升序排序的HTML內容和降序這麼好,但我的腳本與分頁用這個然後每一頁,我必須選擇上行和下行的順序,我想,如果我有點命名爲升序排列,然後在每個頁面名稱將顯示在升序上..排序HTML內容按升序分頁和使用JavaScript

<script type="text/javascript"> 
    var people, asc1 = 1, 
     asc2 = 1, 
     asc3 = 1; 
    window.onload = function() { 
     people = document.getElementById("people"); 
    } 

    function sort_table(tbody, col, asc) { 
     var rows = tbody.rows, 
      rlen = rows.length, 
      arr = new Array(), 
      i, j, cells, clen; 
     // fill the array with values from the table 
     for (i = 0; i < rlen; i++) { 
      cells = rows[i].cells; 
      clen = cells.length; 
      arr[i] = new Array(); 
      for (j = 0; j < clen; j++) { 
       arr[i][j] = cells[j].innerHTML; 
      } 
     } 
     // sort the array by the specified column number (col) and order (asc) 
     arr.sort(function (a, b) { 
      return (a[col] == b[col]) ? 0 : ((a[col] > b[col]) ? asc : -1 * asc); 
     }); 
     // replace existing rows with new rows created from the sorted array 
     for (i = 0; i < rlen; i++) { 
      rows[i].innerHTML = "<td>" + arr[i].join("</td><td>") + "</td>"; 
     } 
    } 
</script> 


      <th onclick="sort_table(people, 0, asc1); asc1 *= -1; asc2 = 1; asc3 = 1;">Name</th> 
      <th onclick="sort_table(people, 1, asc2); asc2 *= -1; asc3 = 1; asc1 = 1;">Address</th> 
      <th onclick="sort_table(people, 2, asc3); asc3 *= -1; asc1 = 1; asc2 = 1;">Contact No.</th> 

      please help 

回答

1

我測試代碼並修改它以正常工作(我們應該跳過th標題):

<script type="text/javascript"> 
var people, asc1 = 1, 
    asc2 = 1, 
    asc3 = 1; 
window.onload = function() { 
    people = document.getElementById("people"); 
} 

function sort_table(tbody, col, asc) { 
    var rows = tbody.rows, 
     rlen = rows.length, 
     arr = new Array(), 
     i, j, cells, clen; 
    // fill the array with values from the table 
    for (i = 1; i < rlen; i++) { // *** i = 1 to skip th headers 
     cells = rows[i].cells; 
     clen = cells.length; 
     arr[i-1] = new Array(); // *** i-1 
     for (j = 0; j < clen; j++) { 
      arr[i-1][j] = cells[j].innerHTML; // *** i-1 
     } 
    } 
    // sort the array by the specified column number (col) and order (asc) 
    arr.sort(function (a, b) { 
     return (a[col] == b[col]) ? 0 : ((a[col] > b[col]) ? asc : -1 * asc); 
    }); 
    // replace existing rows with new rows created from the sorted array 
    for (i = 1; i < rlen; i++) { // *** i = 1 
     rows[i].innerHTML = "<td>" + arr[i-1].join("</td><td>") + "</td>"; //*** i-1 
    } 
} 
</script> 
+0

的代碼修改的部分用'// ***'的符號表示 –