簡體   English   中英

排序 HTML 表,空單元格放置在底部

[英]Sorting HTML table with empty cells placed at the bottom

我在這里使用這個答案 -使用 jquery 對 html 表進行排序,以正確排序我的 HTML 表。 但是,我想簡單地對底部所有空 TD 單元格的表格進行排序。 有沒有人有一個簡單的調整來做到這一點? 這是我的確切代碼-

   $('#sortAnniv').click(function() {
    var table = $('#myTable')
    var rows = table.find('tr:gt(0)').toArray().sort(comparer($(this).index()))
    this.asc = !this.asc
    for (var i = 0; i < rows.length; i++){table.append(rows[i])}
})

function comparer(index) {
    return function(a, b) {
        var valA = getCellValue(a, index), valB = getCellValue(b, index)
        return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.toString().localeCompare(valB)
    }
}
function getCellValue(row, index){ return $(row).children('td').eq(index).text() }

只需為它添加一個案例

function comparer(index) {
    return function(a, b) {
        var valA = getCellValue(a, index), valB = getCellValue(b, index)
        // Add this line
        if (valA.length === 0) return 1; if (valB.length === 0) return -1;
        return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.toString().localeCompare(valB)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM