简体   繁体   English

jquery 数据表禁用特定行中的排序

[英]jquery datatable disable sort in specific row

How to disable sorting in specific row/column in jquery datatable using a class?如何使用类禁用 jquery 数据表中特定行/列的排序?

here's my sample table;这是我的示例表;

    <table>
    <thead>
    <tr>
    <th class="sorting_disabled">Title1</th>
    <th class="">Title2</th>
    <th class="sorting_disabled">Title3</th>
    </tr>
    </thead>
    <tbody>
    <tr><td>Tag 1</td><td>Date 1</td><td>Date 2</td></tr>
    <tr><td>Tag 2</td><td>Date 2</td><td>Date 2</td></tr>
    <tr><td>Tag 3</td><td>Date 3</td><td>Date 3</td></tr>
    <tr><td>Tag 4</td><td>Date 4</td><td>Date 4</td></tr>
    <tr><td>Tag 5</td><td>Date 5</td><td>Date 5</td></tr>
....
    </tbody>
    </table>

script;脚本;

$('.sortable thead tr th.sorting_disabled').livequery(function() {
       $(this).removeClass('sorting');
       $(this).unbind('click');
    });

above code works but if I click to the next column who has a sorting its shows again an arrow.上面的代码有效,但是如果我单击到具有排序的下一列,它会再次显示一个箭头。 though its not clickable;(虽然它不可点击;(

How can I disable the sorting by using a class and not using/redraw a table.如何通过使用类而不使用/重绘表格来禁用排序。

You can disable the sorting using a class in definition.您可以使用定义中的类禁用排序。 Just add this code to the datatable initialization:只需将此代码添加到数据表初始化中:

// Disable sorting on the sorting_disabled class
"aoColumnDefs" : [ {
    "bSortable" : false,
    "aTargets" : [ "sorting_disabled" ]
} ]
$('#example').dataTable( {
  "aoColumnDefs": [
      { 'bSortable': false, 'aTargets': [ 1 ] }
   ]});

That should do it..;)应该这样做..;)

The only solution: First add class="sorting_disabled" to any <th> that you want to disable sorting, then add this code to the datatable initialization:唯一的解决方案:首先将class="sorting_disabled"添加到任何要禁用排序的<th> ,然后将此代码添加到数据表初始化中:

        // Disable sorting on the sorting_disabled class
        "aoColumnDefs" : [ {
            "bSortable" : false,
            "aTargets" : [ "sorting_disabled" ]
        } ],
        "order": [
            [1, 'asc']
        ],

As said in the Datatables documentation :正如数据表文档中所说:

As of DataTables 1.10.5 it is now possible to define initialisation options using HTML5 data-* attributes.从 DataTables 1.10.5 开始,现在可以使用 HTML5 data-* 属性定义初始化选项。 The attribute names are read by DataTables and used, potentially in combination with, the standard Javascript initialisation options (with the data-* attributes taking priority).属性名称由 DataTables 读取并使用,可能与标准 Javascript 初始化选项(data-* 属性优先)结合使用。

Example:例子:

<thead>
    <tr>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Age</th>
        <th data-orderable="false">Start date</th>
        <th>Salary</th>
    </tr>
</thead>

I strongly recommend using this approach, as it is more cleaner than others.我强烈建议使用这种方法,因为它比其他方法更清洁。 DataTables 1.10.15 was originally released on 18th April, 2017. DataTables 1.10.15 最初于 2017 年 4 月 18 日发布。

try the following answer.it works for me.尝试以下答案。它对我有用。

<table class="tablesorter" id="tmp">
<thead>
    <tr>
        <th>Area</th>
        <th>Total Visitors</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>Javascript</td>
        <td>15</td>
    </tr>
    <tr>
        <td>PHP</td>
        <td>3</td>
    </tr>
    <tr>
        <td>HTML5</td>
        <td>32</td>
    </tr>
    <tr>
        <td>CSS</td>
        <td>14</td>
    </tr>
    <tr>
        <td>XML</td>
        <td>54</td>
    </tr>
</tbody>
<tfoot>
    <tr class="no-sort">
        <td><strong>Total</strong></td>
        <td><strong>118</strong></td>
    </tr>
</tfoot>

source: http://blog.adrianlawley.com/tablesorter-jquery-how-to-exclude-rows来源: http ://blog.adrianlawley.com/tablesorter-jquery-how-to-exclude-rows

<th class="sorting_disabled">&nbsp;</th>

$(document).ready(function () {
    $('#yourDataTableDivID').dataTable({
        "aoColumnDefs": [
           {
               "bSortable": false,
               "aTargets": ["sorting_disabled"]
           }
        ]
    });
});

I hope below code works in your case.我希望下面的代码适用于您的情况。

        $("#dataTable").dataTable({
            "aoColumns": [{"bSortable": false}, null,{"bSortable": false}]
        });

You need to disable sorting via "bSortable" for that specific column.您需要通过该特定列的“bSortable”禁用排序。

$(document).ready(function() {
    $('#example').dataTable( {
        "aoColumns": [
            { "bSortable": false },
            null,
            { "bSortable": false }
        ]
    });
});

I came with almost the same solution like in the question, but I used the "fnHeaderCallback".我提出了与问题中几乎相同的解决方案,但我使用了“fnHeaderCallback”。 As far as I understood, it gets called after each header redisplay, so no more worries about 'sorting' class that appears again after clicking on column next to target column.据我所知,它在每次重新显示标题后都会被调用,因此不必再担心单击目标列旁边的列后再次出现的“排序”类。

$('.datatable').dataTable({
  "fnHeaderCallback": function() {
    return $('th.sorting.sorting_disabled').removeClass("sorting").unbind("click");
  }
});

Additional documentation about callbacks: http://datatables.net/usage/callbacks有关回调的其他文档:http: //datatables.net/usage/callbacks

this code worked for me in react.这段代码对我有用。

in row created i added fixed-row class to the row i wanted to stay fixed and not sortable and i drawcallback i hid the row then i appended it to the table itself.在创建的行中,我将固定行类添加到我想保持固定且不可排序的行中,我绘制回调我隐藏了该行,然后我将它附加到表本身。

Hope this works for you:希望这对你有用:

$(this.refs.main).DataTable({
        dom: '<"data-table-wrapper"t>',
        data: data,
        language: {
            "emptyTable": "Loading ...",

        },
        columns,
        ordering: true,
        order: [0,'asc'],
        destory:true,
        bFilter: true,
        fixedHeader: {
            header: true
        },
        iDisplayLength: 100,
        scrollY: '79vh',
        ScrollX: '100%',
        scrollCollapse: true,
        "drawCallback": function( settings ) {
            var dataTableId = $("#To_Scroll_List").find(".dataTables_scrollBody table").attr("id");
            $("..fixed-row").css('display','none');
            $("#"+dataTableId+"_wrapper table").find('tbody tr:last').after($('.fixed-row'));
            $(".fixed-row").show();
        },
        createdRow: function (row, data, index) {
                if(data.UnitsPerLine == 999){
                    $(row).addClass('fixed-row');
                } 
        },
        initComplete: function (settings, json) {

           $("#To_Scroll_List").find(".dataTables_scrollBodytable").attr("id");
            $("#"+dataTableId+" thead tr").remove();



            });

            DatatableSearch(dataTableId+"_wrapper table", "AverageUnitsPerLineReport");
        }     
    });  
}

Without using class, you can follow these steps:在不使用类的情况下,您可以按照以下步骤操作:

  1. Remove the row which has to remain unsorted from the body of the table.从表体中删除必须保持未排序的行。
  2. Include the row to be added in the footer of the table if it is the last row.如果它是最后一行,则将要添加的行包括在表的页脚中。

I did it including the code below in drawCallback:我在 drawCallback 中包含以下代码:

drawCallback: function(settings) {
    let td = $("td:contains('TOTAL')");
    if (td.length) {
        let row = td.closest('tr');
        let clonedRow = row.clone();
        row.remove();
        $('table tbody').append(clonedRow);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM