2017-05-30 54 views
0

我想表與這兩個詞和數字列進行排序的表格。這些詞是按照預期工作的。但數字 - 部分是有點關閉。試圖梳理與數字

其中一列是價格列從500-1500金額。但現在,500下降到1500以下,我不知道如何解決這個問題。

function sortTable(n) { 
    var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0; 
    table = document.getElementById("allCars"); 
    switching = true; 

    dir = "asc"; 

    while (switching) { 
     switching = false; 
     rows = table.getElementsByTagName("TR"); 
     for (i = 1; i < (rows.length - 1); i++) { 
     shouldSwitch = false; 
     x = rows[i].getElementsByTagName("TD")[n]; 
     y = rows[i + 1].getElementsByTagName("TD")[n]; 
     if (dir == "asc") { 
      if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) { 
      shouldSwitch= true; 
      break; 
     } 
     } else if (dir == "desc") { 
     if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) { 
      shouldSwitch= true; 
      break; 
     } 
     } 
    } 
    if (shouldSwitch) { 
     rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); 
     switching = true; 
     switchcount ++; 
    } else { 
     if (switchcount == 0 && dir == "asc") { 
     dir = "desc"; 
     switching = true; 
     } 
    } 
    } 
} 
+0

你的排序是打破了整數,但正在爲字符串的原因是因爲它的排序整數作爲一個字符串,因此,比較1500〜500的問題是,1小於5。這個答案解釋比較詳細(更好)https://softwareengineering.stackexchange.com/a/127644 也許考慮它定義它的類型表的頂級行(TH)的屬性,那麼你可以使用parseInt函數解析,你知道的值將是一個int – Robbie

+1

而不是手動執行此操作,dataTables(datatables.net)會更好。但如果你是反對的是,檢查它是否是一個數字,然後用數(VAL)的值轉換爲數字 – Simon

回答

0

出現這種情況的原因,你的排序方法的順序數值爲一個字符串,所以1500到來之前5

你需要轉換之前比較數值500的原因1來過。

你需要的東西是這樣的:(間HERE評論)

function sortTable(n) { 


var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0; 
    table = document.getElementById("allCars"); 
    switching = true; 

    dir = "asc"; 

    while (switching) { 
     switching = false; 
     rows = table.getElementsByTagName("TR"); 
     for (i = 1; i < (rows.length - 1); i++) { 
     shouldSwitch = false; 
     x = rows[i].getElementsByTagName("TD")[n]; 
     y = rows[i + 1].getElementsByTagName("TD")[n]; 
     if (dir == "asc") { 
      if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) { 
      shouldSwitch= true; 
      break; 
     } 
     } else if (dir == "desc") { 
     // START HERE 
     var value1 = x.innerHTML; 
     var value2 = y.innerHTML; 

     //CHECK IF VALUES ARE NUMERIC. (LINK LATER) 

     if (isNumeric(value1) && isNumeric(value2)){ 
      if(Number(value1) < Number(value2){ 
       shouldSwitch = true; 
       break; 
      } 
     } else { 

     // FINISH HERE 

      if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) { 
       shouldSwitch = true; 
       break; 
     } 
     } 
    } 
    if (shouldSwitch) { 
     rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); 
     switching = true; 
     switchcount ++; 
    } else { 
     if (switchcount == 0 && dir == "asc") { 
     dir = "desc"; 
     switching = true; 
     } 
    } 
    } 
} 

如你本身,我的代碼沒有實現ISNUMERIC(值)功能,可以閱讀validate decimal numbers in JavaScript來實現它!

+0

感謝您抽出時間來回答。這次我沒有得到它的工作,截止日期過去了:)但下次我會記住這一點。 –