数据表不创建某些行 [英] Datatable don't create certain rows

查看:100
本文介绍了数据表不创建某些行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个不创建特定行的回调吗?

There is a callback to not create specific rows ?

这是我的数据表对象:

 $('#table').DataTable({
    dom: "t<'col-sm-5'i><'col-sm-7'p>",
    autoWidth: false,
    serverSide: true,
    aaSorting: [[8, 'desc']],
    beforeCreatedRow: function(row, data) {
        if ( data.field == "OFF" ) {
            // DON'T CREATE THIS ROW
        }
    },
    rowId: 'id',
    lengthChange: false,
    iDisplayLength: 10,
    columns: [
        {data: "column1"},
        {data: "column2"},
        {data: "column3"},
        {data: "column4"},
        {data: "column5"},
        {data: "column6"}
    ]
});

我仅以beforeCreatedRow事件为例.我不确定此事件是否存在.只是为了向您展示我的需求.

I used beforeCreatedRow event just for the example. I'm not sure this event exist. It's just to show you what I need.

因此,如您所见,如果data.field值为OFF,则无需创建行

So as you can see I need to not create the row if data.field value is OFF

推荐答案

我不会在DOM中填充无用的行.不用隐藏它们,只需跳过它们:

I would not populate the DOM with useless rows. Instead of hiding them, simply skip them :

ajax: {
  url: 'some/url/',
  dataSrc: function(d) {
    var data = d.filter(function(row) {
      if (row.field != "OFF") return row
    })
    return data
  }
}


您可以通过挂钩之前到dataSrc的不需要的行> xhr.dt 事件:


You can remove not needed rows prior to dataSrc by hooking into the xhr.dt event :

$('#example').on('xhr.dt', function(e, settings, json, xhr ) {
  json.data = json.data.filter(function(row) {
     if (row.field != 'OFF') return row
  })
})

在此处查看演示-> http://jsfiddle.net/g4h7950t/

see a demo here -> http://jsfiddle.net/g4h7950t/

注意:上面的示例使用的是{ data: [..] } src.简直就是我手上的东西.

Note: The example above is using a { data: [..] } src. Was simply what I had on hand.

这篇关于数据表不创建某些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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