2016-05-12 60 views
0

任何人都可以帮我排序问题我有DataTables。我需要按降序对我的表的第一列进行排序。该列包含一些带有无序列表的单元格。我想要的是,它使用第一个列表项的内容对无序列表中的单元格进行排序。我使用columnDefs类型html,但这不足以解决此问题。该排序系统地将单元格与表格底部的无序列表进行排序,而不是按逻辑顺序排列。Datatables html排序与包含无序列表的数据

我已经建立了一个的jsfiddle来说明这个问题:https://jsfiddle.net/lbriquet2/ka59nb5q/

$(document).ready(function() { 
$('#example').DataTable({ 
"order": [[ 0, "desc" ]], 
"columnDefs": [ 
     {"type": "html", "targets": [ 0 ] }, 
     {"width": "50%", "targets": [ 0, 1 ] } 
     ]  
}  
);}); 

预先感谢您的帮助!

回答

0

可以扩展与自定义的功能分类要做到这一点,就像这样:

// register custom sorting function 
$.fn.dataTable.ext.order['dom-list'] = function (settings, col) 
{ 
    return this.api().column(col, {order:'index'}).nodes().map(function (td, i) { 
     // if there is ul>li we get the value of first 
     var $first_li = $('ul li:first', td); 
     if($first_li.length > 0) { 
      return $first_li.text(); 
     } 
     // there is no ul>li in td we get its value 
     return $(td).text(); 
    }); 
}; 

$(document).ready(function() { 
    $('#example').DataTable({ 
      "order": [[ 0, "desc" ]], 
      "columns": [ 
       { "orderDataType": "dom-list" },// set order type to custom function 
       null 
      ], 
      "columnDefs": [ 
       {"width": "50%", "targets": [ 0, 1 ] } 
      ] 
     } 
    ); 
}); 
+0

太谢谢你了!这完美的作品! :) – lbriquet

+0

我唯一的进步就是我感动orderDataType到ColumnDefs “columnDefs”: \t \t \t \t { “orderDataType”: “DOM的名单”, “目标”:0}, \t \t \t \t { “宽度”:“50%”,“目标”:[0,1]} \t \t \t] – lbriquet