javascript
  • jquery
  • sorting
  • datatables
  • 2012-06-19 62 views 1 likes 
    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']]); 
    
    }); 
    

    回答

    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']]); 
    
    }); 
    
    相关问题