从jQuery DataTables中的呈现跳过一行 [英] Skipping a row from rendering in jquery DataTables

查看:68
本文介绍了从jQuery DataTables中的呈现跳过一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要在初始化期间满足条件,我想跳过行渲染,但是我不知道确切地将其放置在何处.

I want to skip row rendering if a condition is met during its initialization, however I dont know where exactly to place it.

我应该将其放入fnCreatedRow还是fnPreDrawCallback?
我该怎么办?

Should I put it in fnCreatedRow or fnPreDrawCallback?
And how can I do that?

这是我的代码:

 var users_tbl =$('#users_tbl');
    users_tbl.DataTable({
        "deferRender": true,
        "autoWidth": false,
        //for reinitialisation purpose
        destroy: true,
        "aLengthMenu": [[20, 40, 50, 100, -1], [20, 40, 50, 100, "All"]],
        "order": [[ 0, "DESC" ]],
        "ajax" : {
                url :  Main.Vars.host + "settings/get_users",
                type : "GET",
            },
            "aoColumnDefs": [

                    { "sWidth": "5%", "aTargets": [ 0 ] },
                    { "sWidth": "20%", "aTargets": [ 1 ] },
                    { "sWidth": "25%", "aTargets": [ 2 ] },
                    { "sWidth": "15%", "aTargets": [ 3 ] },
                    { "sWidth": "5%", "aTargets": [ 4 ] },
            ],
           "fnCreatedRow"  : function( nRow, aData, iDataIndex ){
                $(nRow).addClass('item-context');   

               return false;
            },
            "fnPreDrawCallback": function( oSettings ) {
                console.log(oSettings);
            },
            "columns": [
                { 
                    "data": "id",
                },
                { 
                    "data": "username",
                },  
                {
                   "render": function(data,type,row,meta) {
                        var owner = row.pnp_info.first_name + " " + row.pnp_info.last_name;
                        return owner;
                   }  
                },
                {
                    "data": "created_on",   

                },
                {
                   "render": function(data,type,row,meta) {
                        return row.active == 1 ? "YES" : "NO";
                   }  
                },

            ],

            sPaginationType: "full_numbers",
    });

推荐答案

对于fnCreatedRow,为时已晚,对于fnPreDrawCallback,您最终取消了表的呈现.您有两种不同的方式:

For fnCreatedRow it is too late, and for fnPreDrawCallback you just end up cancelling rendering of the table. You have two different ways :

1 )在 ajax.dataSrc 回调:

var table = $('#example').DataTable( {
    ajax : {
        url : 'test.json',
        dataSrc: function(json) {
            var rows = [];
            for (var i=0;i<json.data.length;i++) {
                //skip rows "if a condition is met"
                //here just any rows except row #1
                if (i>0) rows.push(json.data[i]);
            }
            return rows;
        }
    }
    ....
})    

2 )根据 xhr 清除JSON > 事件:

table.on('xhr.dt', function (e, settings, json, xhr) {
    //manipulate the json directly, no return needed
    //delete row #1, same as above
    json.data.splice(0,1);
});

两个示例均假设您具有(简化)表单上格式正确的JSON

Both examples is under assumption that you have wellformed JSON on the (simplified) form

{
 "data": [
     {
       "id": "2423",
       "username" : "joe"
     },
     {
       "id": "4321",
       "username" : "gordon"
     }
  ]
}

这篇关于从jQuery DataTables中的呈现跳过一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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