【问题标题】:Plugin Datatables: selecting data in a column to be sorted插件数据表:选择要排序的列中的数据
【发布时间】:2012-06-19 11:32:52
【问题描述】:

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

});

【问题讨论】:

    标签: javascript jquery sorting datatables


    【解决方案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']]);
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-28
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多