[英]Sorting HTML table with empty cells placed at the bottom
I'm using this answer here - sort html table with jquery to properly sort my HTML table.我在这里使用这个答案 -使用 jquery 对 html 表进行排序,以正确排序我的 HTML 表。 However, I would like to simply sort the table with all of the empty TD cells at the bottom.但是,我想简单地对底部所有空 TD 单元格的表格进行排序。 Does anyone have a simple tweak to do this?有没有人有一个简单的调整来做到这一点? Here's my exact code -这是我的确切代码-
$('#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() }
just add a case for it只需为它添加一个案例
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.