2016-11-18 63 views
0

我有一個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(); 

我做了一些谷歌上搜索,看看是否有人問類似這樣的,但我無法找到一個適合我需要的究竟是什麼。他們大多告訴別人使用插件,但我不想添加任何插件。我想有我自己的排序代碼。謝謝。

+1

有一個名爲[DataTables](https://datatables.net/)的jQuery插件。軟件開發是關於代碼重用以及何時明智地使用或不使用插件。在你的情況下,建議一個插件。 –

+0

[在JavaScript中通過字符串屬性值對對象數組排序](http://stackoverflow.com/a/35092754/215552) –

回答

3

您可以填寫0你的電話號碼,以獲得一個排序字符串:

var myNumber = 12; 
var filler = "00000000000"; 
var res = filler.substr(0, filler.length - myNumber.toString().length) + myNumber; 

通常情況下,RES = 0000000012

如果需要用parseInt函數你可以有你的電話號碼回(RES,10)

此代碼在您的代碼段中工作。

var findSortKey = function($cell) { 
     var sk = $cell.find('.sort-key').text().toUpperCase() + ' ' + $cell.text().toUpperCase(); 
     var ik = parseInt(sk,10); 
     return ik != NaN ? ik : sk; 
    }; 
+0

如何獲取所有信息以從數據庫中填充表格?只需將myNumber設置爲空號碼? – lostInTheTetons

+0

或簡單的'parseInt(res,10)'不要忘記基數...這個數字將被解析爲10而不是12,因爲這是一個八進制數(前導'0')。 –

+0

你應該通過這種方式來改變你的排序關鍵字。 – asfez

相關問題