如何在JQUERY Datatable

时间:2016-03-09 04:32:15

标签: jquery datatables

我尝试仅在数据表中启用基于一列的排序。但它不起作用。这是我尝试的方式

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

中尝试

4 个答案:

答案 0 :(得分:19)

将类no-sort添加到除要排序的列之外的所有<th> ..请检查https://jsfiddle.net/24zztcL9/

我只为第二列启用排序&#34;位置&#34;

<强> 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"
    }]

  });
});

答案 1 :(得分:3)

而不是使用columnDefs我更喜欢使用columns这样:

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

Working JSFiddle

无论哪种方法都有效,我只是更喜欢columns数组的粒度控制。

答案 2 :(得分:0)

对我来说,“接受的答案”无效

因为在我的表中所有<th>都具有相同的类,因此我无法向<th>添加其他类,也无法删除<th>的默认类,

我的桌子的结构如下:

       <table id="example" class="mt-table" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th class="mt-head">Name</th>
                    <th class="mt-head">Position</th>
                    <th class="mt-head">Office</th>
                    <th class="mt-head">Age</th>
                    <th class="mt-head">Start date</th>
                    <th class="mt-head">Salary</th>
                </tr>
            </thead>
            <tbody>
             <tr>
               <td>Hari</td>
               <td>Senior Engineer</td>
               <td>Ahmedabad</td>
               <td>20</td>
               <td>11/02/2019</td>
               <td>50,000</td>
             </tr>
            </tbody>
         </table>

现在我也只想为Position列启用排序选项。

我所做的如下:

在我的<script>

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

    "ordering": true,
    columnDefs: [
    {
        "orderable": true,
        "targets": 1,
    },
    {
      orderable: false,
      targets: "mt-head"
    }]

  });
});

现在,只有 Position (位置)列可以订购,而其他列则不能。

因此规则是将要排序的列首先放在columnDefs中,如上例所示,然后您可以放置类选择器以将其禁用在所有其他列上进行排序

答案 3 :(得分:0)

$(function () {
       $("#dgUsers").prepend($("<thead></thead>").append($("#dgUsers").find("tr:first"))).DataTable({
           "aoColumnDefs": [
             { "bSortable": false, "aTargets": [ 4, 5, 6 ] }
           ] }
       );
   });

尝试一下...

  

4、5、6是从0开始的列号。

相关问题