我有一个包含如下容量列的数据表:
<table id="datatable" class="table">
<thead> <tr> <th>N</th> <th>capa</th> </tr> </thead>
<tbody>
<tr>
<td>1</td>
<td>2 Go</td>
</tr>
<tr>
<td>2</td>
<td>1 To</td>
</tr>
<tr>
<td>3</td>
<td>320 Go</td>
</tr>
<tr>
<td>4</td>
<td>2 To</td>
</tr>
<tr>
<td>5</td>
<td>500 Go</td>
</tr>
</tbody>
</table>
<script>
$(document).ready(function() {
$('#datatable').dataTable({
'aaSorting': [],
'iDisplayLength': 50,
'aLengthMenu': [[10, 25, 50, 100, 500, -1], [10, 25, 50, 100, 500, 'Tous']]
});
});
</script>
我正在尝试对其进行排序以获得此结果:
2 Go
320 Go
500 Go
1 To
2 To
但无法通过阅读排序插件文档来弄清楚如何做到这一点。
谢谢
最佳答案
好的,终于明白了
http://jsfiddle.net/jkwoaj3x/1/
$('#datatable').dataTable({
"columns": [
null,
{ "orderDataType": "custom-sort" }
]
});
这是您的自定义排序函数
$.fn.dataTable.ext.order['custom-sort'] = function ( settings, col )
{
return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
console.log($(td).text().replace(/[^0-9\.]+/g, ''));
return $(td).text().replace(/[0-9]/g, '');
} );
}
这是你的解决方案吗?
关于javascript - 如何对数据表中的容量列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29124490/