2016-03-09 27 views
4

我試圖僅在數據表中根據一列啓用排序。但它不起作用。這是我嘗試如何啓用僅針對JQUERY數據表中的一列進行排序

var myTable = $("#tbl_main").dataTable({ 
    "dom": "<'tableinfobar'i><'tablesearchbox'f><'tablestarfilter'><'tablebody't>S<'tablelength'l><'tablepaging'p>", 
    "ordering": false, 
    "columnDefs": [{"targets": [0, 6],"searchable": false}, {"targets":2, "type":"html-num","orderable":true}], 
    "lengthMenu": [ 
     [10, 20, 50, 100, 500, -1], 
     [10, 20, 50, 100, 500, "All"] 
    ] 
}).show(); 

來這裏的路上我需要啓用僅僅次於列排序,並試圖在columnDefs

+0

嘗試像' 「訂貨」:真 「columnDefs」:[{ aTargets:[ '_all'],bSortable:假}, {aTargets:[1],bSortable:真正} ] – RRR

回答

12

添加類no-sort所有<th>除了列要排序..查收https://jsfiddle.net/24zztcL9/

我有那種只爲第2列 「位置」

HTML啓用

<table id="example" class="display" cellspacing="0" width="100%"> 
     <thead> 
      <tr> 
       <th class="no-sort">Name</th> 
       <th>Position</th> 
       <th class="no-sort">Office</th> 
       <th class="no-sort">Age</th> 
       <th class="no-sort">Start date</th> 
       <th class="no-sort">Salary</th> 
      </tr> 
     </thead> 
    </table> 

jQuery的

$(document).ready(function() { 
    $('#example').DataTable({ 

    "ordering": true, 
    columnDefs: [{ 
     orderable: false, 
     targets: "no-sort" 
    }] 

    }); 
}); 
3

而不是使用columnDefs我更喜歡使用columns這樣的:

$(document).ready(function() { 
    $('#example').DataTable({ 
     "columns":[ 
      { 
       "sortable": false 
      }, 
      { 
       "sortable": true 
      }, 
      { 
       "sortable": false 
      }, 
      { 
       "sortable": false 
      }, 
      { 
       "sortable": false 
      }, 
      { 
       "sortable": false 
      } 
     ] 
    }); 
}); 

Working JSFiddle

無論是pproach的作品,我只是喜歡columns陣列的粒度控制。

相關問題