1

我是 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']]);

});
4

1 回答 1

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']]);

});
于 2012-06-19T12:25:49.310 回答