Jquery DataTables 排除具有特定类的行 [英] Jquery DataTables to exclude rows with a certain class

查看:19
本文介绍了Jquery DataTables 排除具有特定类的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 HTML 表格,在其中应用了 DataTables 函数.我使用表格的第一行,并将类模板"用作我的模板行.然后选择此格式并使用 JSON 提要填充表中的所有行.问题是 DataTables 提供的分页包括这个隐藏的模板行,所以总是让我的第一页显示的行比所有其他页少 1.

有没有办法排除任何行(其中只有一个)并将类模板"应用于 tr?

<link href="/bower_components/datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.css" rel="stylesheet"><!-- 数据表响应式 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"><头><tr><th class="orderable asc">Name</th><th class="orderable no-sort" width="10%">有用吗?</th><th class="orderable" width="15%">创建</th><th class="orderable c" width="10%">直播?</th><th class="r no-sort" width="12%">Actions</th></tr></thead><tr id="problem_#PROBLEMID#" class="模板#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>

$(document).ready(function() {延迟(功能(){$('#problem_table').DataTable({响应:真实,页长:20,aLengthMenu: [[20, 40, 60, -1], [20, 40, 60, "All"]],aoColumnDefs : [{ "bSortable" : false, "aTargets" : [ "no-sort" ] }]});}, 200 );});

解决方案

您可以使用旧的 自定义行过滤器:

$.fn.dataTableExt.afnFiltering.push(函数(oSettings,aData,iDataIndex){var row = oSettings.aoData[iDataIndex].nTr;返回 $(row).hasClass('template') ?假:真;});

即使它是 1.10.x 之前的匈牙利符号,它仍然适用于 DataTable() 实例.

演示 -> http://jsfiddle.net/zaxkrc49/

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 );

});

解决方案

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/

这篇关于Jquery DataTables 排除具有特定类的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆