Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery DataTables to exclude rows with a certain class

Tags:

datatables

I have an HTML table in which I have applied the DataTables function to. I use the first row of the table with the class 'template' applied as my template row. Then pick this formatting up and populate all the rows in the table using a JSON feed. The problem is that the pagination provided by DataTables includes this hidden template row so always makes my first page display 1 less row than all the others.

Is there a way to exclude any rows (of which there will only be one) with the class 'template' applied to the tr?

<!-- DataTables CSS -->
<link href="/bower_components/datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.css" rel="stylesheet">

<!-- DataTables Responsive CSS -->
<link href="/bower_components/datatables-responsive/css/dataTables.responsive.css" rel="stylesheet">

<div class="alert-message"></div>

<div class="dataTable_wrapper">

    <table class="loadtable table table-hover table-stripped" id="problem_table" data-page="0" data-params="" data-orderby="p.name" data-orderdir="DESC" data-url="/admin/problem/ajax/tables/problem" cellpadding="0" cellspacing="0" border="0">
        <thead>
            <tr>
                <th class="orderable asc">Name</th>
                <th class="orderable no-sort" width="10%">Helpful?</th>
                <th class="orderable" width="15%">Created</th>
                <th class="orderable c" width="10%">Live?</th>
                <th class="r no-sort" width="12%">Actions</th>
            </tr>
        </thead>
        <tbody>
            <tr id="problem_#PROBLEMID#" class="template #ROWCLASS#">
                <td class="orderable asc">#NAME#</td>
                <td class="orderable"><span class="fa fa-thumbs-o-up"> #UP_VOTE#</span> <span class="fa fa-thumbs-o-down"> #DOWN_VOTE#</span></td>
                <td class="orderable">#CREATED#</td>
                <td class="orderable c"><span class="fa #IS_LIVE#"></span></td>
                <td class="r last">#ACTIONS#</td>
            </tr>
         </tbody>
    </table>
</div>
$(document).ready(function() {

    delay( function() {
        $('#problem_table').DataTable({
            responsive: true,
            pageLength: 20,
            aLengthMenu: [[20, 40, 60, -1], [20, 40, 60, "All"]],
            aoColumnDefs : [{ "bSortable" : false, "aTargets" : [ "no-sort"     ] }]
        });
    }, 200 );

});
like image 743
Eth Avatar asked Apr 13 '15 09:04

Eth


1 Answers

You can use the good old custum row filter for this :

$.fn.dataTableExt.afnFiltering.push(
    function( oSettings, aData, iDataIndex ) {
        var row = oSettings.aoData[iDataIndex].nTr;
        return $(row).hasClass('template') ? false : true;
    }
);

Even though it is pre-1.10.x hungarian notation, it still works with DataTable() instances.

demo -> http://jsfiddle.net/zaxkrc49/

like image 68
davidkonrad Avatar answered Oct 15 '22 00:10

davidkonrad