2016-04-06 4 views
0

私のprevious question私はページをロードする際にテーブルをソートしようとしていましたが、トグルスイッチをソートすることなくテーブルをソートしようとしていました。ページ。jQuery on pageロードソートは先行通貨記号でソートされません

質問に答えて、私はかなり良い反応を得て、問題を解決して眠りについたと思ったので、今日私のウェブサイトでコードを使用しようとすると、ソート技術は機能しません完全にjsfiddle https://jsfiddle.net/ghzch66e/12/で動作します。

ウェブページ上のデータには先頭の(£)記号https://jsfiddle.net/ghzch66e/13/が含まれているため、私は自分のウェブサイトでテーブルをソートしないことに気付きました。

どのように先頭の(£)記号で表の並べ替えを行うことができます。

jQuery(document).ready(function(e) { 
var dataRows = []; 

//Create an array of all rows with its value (this assumes that the amount is always a number. You should add error checking!! Also assumes that all rows are data rows, and that there are no header rows. Adjust selector appropriately. 
$('#internalActivities > tbody > tr').each(function(i,j) { 
    dataRows.push({'amount': parseFloat($(this).find('.amount').text()), 'row': $(this)}); 
}) 

//Sort the data smallest to largest 
dataRows.sort(function(a, b) { 
    return a.amount - b.amount; 
}); 

//Remove existing table rows. This assumes that everything should be deleted, adjust selector if needed :). 
$('#internalActivities').empty(); 

//Add rows back to table in the correct order. 
dataRows.forEach(function(ele) { 
    $('#internalActivities').append(ele.row); 
}) 
}); 
+0

にそれを押したときに「」と「£」を交換してください。 –

+0

これをチェックしましたか? http://stackoverflow.com/questions/22360440/how-to-ignore-and-characters-in-a-value-during-sort-comparison多分、シンボルをヌルに置き換えようとしますか?ちょうど推測。 – s0rfi949

+0

http://stackoverflow.com/questions/23121437/sorting-an-array-in-javascript-85-88-dollars-9-35-95-96-126 – litel

答えて

2

あなたは通貨記号を取り除くparseFloatはを呼び出す前に、アレイ

$('#internalActivities > tbody > tr').each(function(i,j) { 
     dataRows.push({'amount': parseFloat($(this).find('.amount').text().replace(/£/,"")), 'row': $(this)}); 
     }) 
+0

これは今動作します問題があります私のウェブサイトに何かアイデアが消えてしまった理由は何ですか? – Scott

+0

私はあなたの新しいjsfiddleを作成しました。https://jsfiddle.net/ghzch66e/14/ – Scott

+0

ここでこのフィドルを見ればjsfiddleで私を見せてもらえますか(これはあなたが変更できます)https://jsfiddle.net/ghzch66e/14 /私は実際にtbodyをターゲットにしていますが、ヘッダーはまだありません – Scott

関連する問題