user1896653
user1896653

Reputation: 3327

How to disable sorting from some specif rows in DataTables

I've used this table. I need my first row of tbody(which displays average number of the columns) should not be sorted, I mean that row should in top always(below thead) like this: . How can I do this? I search solution on google and mostly I find put that row on the thead or something like that. But, if I try to put that row on that place, it seems so complex to me. So, is there any way put a class on the row and disabling sortng on that classed row? like: <tr class="average no-sort"></tr>

Fiddle

Upvotes: 4

Views: 4068

Answers (4)

Nancy
Nancy

Reputation: 178

  1. Remove the row which has to remain unsorted from body of the table.
  2. Include the row to be added in the footer of the table if it is the last row.

Upvotes: 0

Vadim Pushtaev
Vadim Pushtaev

Reputation: 2353

I create the second tbody in my table and just move all rows I need to persist in that another tbody like so:

initComplete: function() {
    var self = this;

    var api = this.api();
    api.rows().eq(0).each(function(index) {
        var row = api.row(index);
        if (row.data()...) { // condition here
            var $row_to_persist = $(row.node());
            var $clone = $row_to_persist.clone();

            $(self).find('tbody:last').append($clone);

            $total_row.remove();
            row.remove();
            api.draw();
        }
    });
}

Upvotes: 0

sergioBertolazzo
sergioBertolazzo

Reputation: 604

I had the same problem, in my case I solved it this way:

var $tr = $('#yourTable tr.no-sort'); //get the reference of row with the class no-sort
var mySpecialRow = $tr.prop('outerHTML'); //get html code of tr
$tr.remove(); //remove row of table

$('#yourTable').dataTable({
    "fnDrawCallback": function(){
        //add the row with 'prepend' method: in the first children of TBODY
        $('#yourTable tbody').prepend(mySpecialRow);

    }                   
});

Upvotes: 4

user2094477
user2094477

Reputation:

Also looking for a solution for this. It's easy to do by modifying the dom after the table has initiated, but that breaks all of the responsive features.

Need a way to keep rows at the top of the table after sorting and filtering has been done.

Upvotes: 0

Related Questions