javascript - 如何对数据表中的容量列进行排序

标签 javascript sorting datatables

我有一个包含如下容量列的数据表:

<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/

相关文章:

javascript - JavaScript 中的大数

javascript - 从 url 哈希打开并滚动到选项卡(JS/Jquery/Bootstrap)

javascript - 如何在 beforeShow block 中覆盖 this.href? (jquery/花式框)

c# - C# 中的 List.Orderby 首先排序为偶数升序的数字,然后为奇数降序的相同数字

javascript - 是否可以在点击事件上渲染事件模板的一部分?

javascript - 单击复选框时禁用行隐藏/显示

javascript - 为什么 AJAX 的响应为 "_blank"对第一个函数起作用,但对第二个函数不起作用?

javascript - 通过将最后一个值与下一个索引中的第一个值匹配来对 2D 数组进行排序

没有lambda表达式的python排序

javascript - 刷新数据表数据