我有一個HTML表和按字母順序排序表的jQuery代碼,但我無法弄清楚如何以數字順序排列其中一列。它只是按第一個數字排序數字列,而不是較長的數字。所以99上方顯示出來,而不是9340.我如何使用jquery對數據表進行排序
$('thead 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;
//step back up the tree and get the rows with data
//for sorting
var $rows = $(this).parent().parent().parent().find('tbody tr').get();
//loop through all the rows and find
$.each($rows, function(index, row) {
row.sortKey = findSortKey($(row).children('td').eq(column));
});
//compare and sort the rows alphabetically
$rows.sort(function(a, b) {
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');
});
});
function filterDataTable(selector,query){
query = $.trim(query);
query = query.replace(/ /gi, '|');
$(selector).each(function(){
($(this).text().search(new RegExp(query, "i")) < 0) ? $(this).hide().removeClass('visibile') : $(this).show().addClass('visible');
});
}
$('tbody tr').addClass('visible');
$("#filter").keyup(function(event){
if(event.keyCode == 27 || $(this).val() == ''){
$(this).val('');
$('tbody tr').removeClass('visible').show().addClass('visible');
}else {
filter('tbody tr', $(this).val());
}
});
$('#filter').show();
我做了一些谷歌上搜索,看看是否有人問類似這樣的,但我無法找到一個適合我需要的究竟是什麼。他們大多告訴別人使用插件,但我不想添加任何插件。我想有我自己的排序代碼。謝謝。
有一個名爲[DataTables](https://datatables.net/)的jQuery插件。軟件開發是關於代碼重用以及何時明智地使用或不使用插件。在你的情況下,建議一個插件。 –
[在JavaScript中通過字符串屬性值對對象數組排序](http://stackoverflow.com/a/35092754/215552) –