插件数据表:选择要排序的列中的数据

时间:2012-06-19 11:32:53

标签: javascript jquery sorting datatables

我是js中插件DataTables的新手,我没有找到如何选择要在列中排序的特定数据。例如,在我的表中,我有一个“评级”列。我只想用百分比来排序,而不是用其他值来排序。

 <td>
        <span class="rating">100.00%</span>
        <span class="voteup">3 <img src='/images/voteup.png' alt='voteup' /></span>
        <span class="votedown"> 0 <img src='/images/votedown.png' alt='votedown' /></span>
         <br /> 
        <span class="comment">0 comments</span> <br/> 
        <span class="views">17 views</span>
 </td>

我直接从dom(由php生成)加载数据,这是我在js中生成的DataTables。

var oTable;


$(document).ready(function() {

oTable = $('#BuildList').dataTable({
    "aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
    "iDisplayLength": -1,
    "aoColumns": [
        { "bSortable": false},
        { "bSortable": false},
        { "bSortable": false},
        { "asSorting": [ "asc" ] },
        { "asSorting": [ "desc" ] },
    ]
});

// To sort by default the column 4
oTable.fnSort([[3, 'asc']]);

});

1 个答案:

答案 0 :(得分:1)

问题解决了

$.fn.dataTableExt.oSort['rating-desc'] = function(x,y) {

x = parseFloat($(x).first().html());
y = parseFloat($(y).first().html());

return ((x < y) ?  1 : ((x > y) ? -1 : 0));
};
$(document).ready(function() {




oTable = $('#BuildList').dataTable({
    "aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
    "iDisplayLength": -1,
    "aoColumns": [
        { "bSortable": false},
        { "bSortable": false},
        { "bSortable": false},
        { "asSorting": [ "asc" ] },
        { "asSorting": [ "desc" ], "sType": "rating" }
    ]
});

// To sort by default the column 4
oTable.fnSort([[3, 'asc']]);

});