我正在使用jquery DataTable作为数据网格。我需要帮助对可能是字母和数字组合的列值进行排序。排序时,我必须优先考虑字母之前的数字。
$(document).ready(function() {
$('#example').dataTable();
});
在此列 Browser
示例中,ASC
顺序的结果应如下所示:
abc21
bbc22
abc23
bbc26
abc29
abc31
首先按数字排序。
JSFiddle 上的代码
您应该能够使用Formatted Numbers
自定义排序插件,该插件可以在Datatables插件部分找到
实质上,您在对该列进行排序时调用下面的代码。
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"formatted-num-pre": function ( a ) {
a = (a === "-" || a === "") ? 0 : a.replace( /[^d-.]/g, "" );
return parseFloat( a );
},
"formatted-num-asc": function ( a, b ) {
return a - b;
},
"formatted-num-desc": function ( a, b ) {
return b - a;
}
} );
您可以通过在表的 init 代码中设置 aoColumns
对象来告诉数据表使用此排序
$(document).ready(function() {
$('#example').dataTable( {
"aoColumns": [
null, // each of these = a column in your table
null,
null,
// this column, 4th, will be sorted usig the plugin
{ "sType": "formatted-num-pre" },
null
]
} );
} );
同一页面上还有一个Natural sorting
插件可能会有所帮助。
最终,您可以按照示例创建自己的插件。