2016-09-23 110 views
0

我需要通過單擊表頭對錶項進行排序。我的列值編號爲字符串。所以排序並不是我想要的。我知道我應該將我的字符串轉換爲浮動。我需要逗號分隔符和雙精度點。我試圖做parseFloat,但這不起作用。角度表排序

例子。我想排序「123,456.00」,「345」,「-34.00」,「7.78」種字符串。我分享了jsfiddle鏈接來解釋我的場景。 http://jsfiddle.net/k4seL1yx/1/

function SortableTableCtrl() { 
    var scope = this; 
    scope.head = { 
     a: "Poured", 
     b: "Sold", 
     c: "Loss" 
    }; 
    scope.body = [{"ID":"September-2016", "Product":"September-2016", "Poured":"111759.07", "Sold":"107660.97", "Loss":"-4098.10", "Variance":"-3.67", "startDate":"2016-09-01", "endDate":"2016-09-22"}, {"ID":"November-2015", "Product":"November-2015", "Poured":"53690.25", "Sold":"52953.60", "Loss":"-736.65", "Variance":"-1.37", "startDate":"2015-11-20", "endDate":"2015-11-30"}, {"ID":"May-2016", "Product":"May-2016", "Poured":"156401.65", "Sold":"151192.51", "Loss":"-5209.14", "Variance":"-3.33", "startDate":"2016-05-03", "endDate":"2016-05-31"}, {"ID":"March-2016", "Product":"March-2016", "Poured":"49260.22", "Sold":"49399.14", "Loss":"138.92", "Variance":"0.28", "startDate":"2016-03-01", "endDate":"2016-03-09"}, {"ID":"June-2016", "Product":"June-2016", "Poured":"162126.88", "Sold":"161718.62", "Loss":"-408.26", "Variance":"-0.25", "startDate":"2016-06-01", "endDate":"2016-06-30"}, {"ID":"July-2016", "Product":"July-2016", "Poured":"160185.68", "Sold":"154882.40", "Loss":"-5303.28", "Variance":"-3.31", "startDate":"2016-07-01", "endDate":"2016-07-31"}, {"ID":"January-2016", "Product":"January-2016", "Poured":"355509.26", "Sold":"179696.72", "Loss":"-175812.54", "Variance":"-49.45", "startDate":"2016-01-01", "endDate":"2016-01-31"}, {"ID":"February-2016", "Product":"February-2016", "Poured":"150980.73", "Sold":"146248.72", "Loss":"-4732.01", "Variance":"-3.13", "startDate":"2016-02-01", "endDate":"2016-02-29"}, {"ID":"December-2015", "Product":"December-2015", "Poured":"167843.42", "Sold":"163732.95", "Loss":"-4110.47", "Variance":"-2.45", "startDate":"2015-12-01", "endDate":"2015-12-31"}, {"ID":"August-2016", "Product":"August-2016", "Poured":"168853.51", "Sold":"160024.84", "Loss":"-8828.67", "Variance":"-5.23", "startDate":"2016-08-01", "endDate":"2016-08-31"}] 
    scope.sort = { 
     column: 'b', 
     descending: false 
    }; 

    scope.selectedCls = function(column) { 
     return column == scope.sort.column && 'sort-' + scope.sort.descending; 
    }; 

    scope.changeSorting = function(column) { 
     var sort = scope.sort; 
     if (sort.column == column) { 
      sort.descending = !sort.descending; 
     } else { 
      sort.column = column; 
      sort.descending = false; 
     } 
    }; 
} 

回答

0

我發現我的問題的解決方案。實際上,我非常專注於從字符串轉換爲「傾倒,出售,丟失」。但爲什麼我不應該創建一個新的列作爲克隆,並且它的格式是浮動的。 parseFloat完成了魔術。因此,我可以使用Poured值進行表顯示,並使用pouredClone值在後面進行排序。

//Initially I got the above scope.body only. To perform sorting I modified scope.body as like below  
scope.body.forEach(function(value) { 
     value.pouredClone = parseFloat(value.Poured) 
     value.soldClone  = parseFloat(value.Sold) 
     value.lossClone  = parseFloat(value.Loss) 
     value.varianceClone = parseFloat(value.Variance) 
}); 

這裏是小提琴與解決方案的鏈接。 http://jsfiddle.net/q7mcu6fx/1/