-1
用下面的jQuery代碼訂貨錶行,數字與點的數千名格式不正確排序:jQuery的不能爲了成千上萬號點號分隔訂購
這些數字:
1.000, 2.500,4.000,850
被排序爲:
850,4.000,2.500 ,1.000
我需要訂購這些樣品號碼而不刪除點。
jQuery代碼:
$('th').each(function (column) {
$(this).addClass('sortable').click(function() {
var findSortKey = function ($cell) {
return $cell.find('.sort-key').text().toUpperCase()+ ' ' + $cell.text().toUpperCase();
};
var sortDirection = $(this).is('.sorted-asc') ? -1 : 1;
var $rows = $(this).parent().parent().parent().find('tbody tr').get();
var bob = 0;
// Loop through all records and find
$.each($rows, function (index, row) {
row.sortKey = findSortKey($(row).children('td').eq(column));
});
// Compare and sort the rows alphabetically or numerically
$rows.sort(function (a, b) {
if (a.sortKey.indexOf('-') == -1 && (!isNaN(a.sortKey) && !isNaN(a.sortKey))) {
// Rough Numeracy check
if (parseInt(a.sortKey) < parseInt(b.sortKey)) {
return -sortDirection;
}
if (parseInt(a.sortKey) > parseInt(b.sortKey)) {
return sortDirection;
}
} else {
if (a.sortKey < b.sortKey) {
return -sortDirection;
}
if (a.sortKey > b.sortKey) {
return sortDirection;
}
}
return 0;
});
// Add the rows in the correct order to the bottom of the table
$.each($rows, function (index, row) {
$('tbody').append(row);
row.sortKey = null;
});
// Identify the column sort order
$('th').removeClass('sorted-asc sorted-desc');
var $sortHead = $('th').filter(':nth-child(' + (column + 1) + ')');
sortDirection == 1 ? $sortHead.addClass('sorted-asc') : $sortHead.addClass('sorted-desc');
// Identify the column to be sorted by
$('td').removeClass('sorted').filter(':nth-child(' + (column + 1) + ')').addClass('sorted');
});
});
Th這就是爲什麼你***從未***格式化或本地化的日期,數字等排序 – connexo
顯然'1.000'小於'850'。 –
@PraveenKumar您標記爲重複的問題與DataTables有關。這是一個自定義函數,而不是插件。該解決方案不符合我的要求。 – user3472675