按英国日期对 jQuery 数据表进行排序,忽略空单元格
Sort a jQuery datatable by UK date and ignore empty cells
以dd/mm/yyyy
格式对日期进行排序时,如何使空单元格粘在底部?我的问题在这里(对年龄列进行排序):http://jsfiddle.net/dup75/11/
$('#hr_curriculum_interns').dataTable( {
"aoColumns": [
{ "sType": "date-uk" },
null,
null,
null,
null,
null,
null,
null,
null
]
});
这里著名的代码可以在 http://datatables.net/forums/discussion/4025/sorting-to-ignore-empty-cells 哪个代码上看到:
$.fn.dataTableExt.oSort['mystring-asc'] = function(x,y) {
var retVal;
x = x.replace(' ', '');
y = y.replace(' ', '');
if (x == y) retVal = 0;
else if (x.substr(0,1) == "{" && y.substr(0,1) == "{") {
if (x > y) retVal= 1;
else retVal = -1;
}
else if (x.substr(0,1) == "{") retVal = 1;
else if (y.substr(0,1) == "{") retVal = -1;
else if (x > y) retVal= 1;
else return -1;
return retVal;
}
$.fn.dataTableExt.oSort['mystring-desc'] = function(y,x) {
var retVal;
x = x.replace(' ', '');
y = y.replace(' ', '');
if (x == y) retVal= 0;
else if (x.substr(0,1) == "{" && y.substr(0,1) == "{") {
if (x > y) retVal= -1;
else retVal = 1;
}
else if (x.substr(0,1) == "{") retVal = -1;
else if (y.substr(0,1) == "{") retVal = 1;
else if (x > y) retVal = 1;
else return -1;
return retVal;
}
但它并没有解决我以 dd/mm/yyy 格式对我的列"年龄"进行排序的问题。它只是使我的列采用整数格式,这不应该是因为它采用日期格式。
请参阅此处更新的小提琴或下面的 StackSnippet。基本上你需要实现一个自定义排序功能。以下是该排序函数的代码以及说明:
// add a set of custom sorting functions
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"customdatesort-pre": function(a) {
// returns the "weight" of a cell value
var r, x;
if (a === null || a === "") {
// for empty cells: weight is a "special" value which needs special handling
r = false;
} else {
// otherwise: weight is the "time value" of the date
x = a.split("/");
r = +new Date(+x[2], +x[1] - 1, +x[0]);
}
console.log("[PRECALC] " + a + " becomes " + r);
return r;
},
"customdatesort-asc": function(a, b) {
// return values are explained in Array.prototype.sort documentation
if (a === false && b === false) {
// if both are empty cells then order does not matter
return 0;
} else if (a === false) {
// if a is an empty cell then consider a greater than b
return 1;
} else if (b === false) {
// if b is an empty cell then consider a less than b
return -1;
} else {
// common sense
return a - b;
}
},
"customdatesort-desc": function(a, b) {
if (a === false && b === false) {
return 0;
} else if (a === false) {
return 1;
} else if (b === false) {
return -1;
} else {
return b - a;
}
}
});
$(document).ready(function() {
$('#hr_curriculum_interns').dataTable({
"aoColumns": [{
"sType": "customdatesort"
},
null,
null,
null,
null,
null,
null,
null,
null
]
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/css/jquery.dataTables.css">
<div class="container">
<table id="hr_curriculum_interns" class="table table-striped">
<thead>
<tr>
<th>Age</th>
<th>Position</th>
<th>-</th>
<th>-</th>
<th>-</th>
<th>-</th>
<th>-</th>
<th>-</th>
<th>-</th>
</tr>
</thead>
<tbody>
<tr>
<td>31/12/2015</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
</tr>
<tr>
<td>31/12/2014</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
</tr>
<tr>
<td></td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
</tr>
<tr>
<td>14/11/2014</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
</tr>
<tr>
<td>31/12/2013</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
<td>Athos</td>
</tr>
</tbody>
</table>
</div>
相关文章:
- 使用jquery在单击时在单元格中输入值
- CSS-若窗口太小,滚动条会出现在“表格”单元格上
- 可以'我不明白为什么;t将行和单元格添加到表中
- 如何在Angular中的表的所有单元格中添加链接
- 如何使用 jquery sortable 对表的所有行中的表单元格进行排序
- jQuery UI 可排序:检测表单元格中是否已包含项目
- 按英国日期对 jQuery 数据表进行排序,忽略空单元格
- 在单元格中显示另一个值时,按一个值对jqGrid进行排序
- jQuery UI可排序-排序时表格单元格丢失边框
- 如何对具有空单元格的日期列进行排序
- 通过单击另一个表中的单元格对表进行排序
- Bootstrap Datatables按部分单元格内容排序
- jqGrid-拖动一行进行排序会打乱单元格宽度
- ag网格对单元格值的排序发生了变化
- 如何在表排序器中设置固定的表宽度(不是单元格)
- Dojo数据网格:按单元格布局中的第一个值排序
- 我如何得到一个谷歌可视化表上的格式单元格值排序时呈现
- 如何在JavaScript中删除和重新排序表格中的单元格
- 使网格系统中单元格的垂直重新排序具有响应性
- SlickGrid单元格样式在排序时丢失