且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery dataTable 列的自定义排序

更新时间:2023-08-26 21:48:58

通过查看带有 HTML 插件的数字,您可以使用现有代码并修改正则表达式以查找负数而不是剥离所有内容.使用它,您可以将NA"周围的 HTML 标记放在一起,并使用 HTML5 数据内部 ID 来存储集合的最低编号.

By looking at the Numbers with HTML plugin you can take the existing code and modify the regex to look for negative numbers instead of stripping everything. Using that you can put together a HTML tag around the "NA" and use the HTML5 data-internalid to store the lowest number of the collection.

于是变成:

<td><a data-internalid="-1">NA</a></td>

和(注意正则表达式)

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"num-html-pre": function ( a ) {
    var x = String(a).replace(/(?!^-)[^0-9.]/g, "");
    return parseFloat( x );
},

"num-html-asc": function ( a, b ) {
    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},

"num-html-desc": function ( a, b ) {
    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}});

然后在datatable中,设置类型为num-html

Then in the datatable, set the type to num-html

$('table').dataTable({
    "aoColumns": [{ "sType": "num-html" }],
    "aaSorting": [[ 0, "desc" ]],
});

您可以在此处查看我的完整解决方案:http://jsfiddle.net/rYtxh/4/

You can see my full solution here: http://jsfiddle.net/rYtxh/4/