数据表设置默认选项

Datatables setting default options

本文关键字:选项 默认 设置 数据表      更新时间:2023-09-26

这就是初始化dataTable 的方法

$('#example').dataTable( {
  "columnDefs": [ {
      "targets": 0,
      "searchable": false
    } ]
} );

我能做下面这样的事情吗?

var mytable = $('#example').dataTable();
mytable.columnDefs({
  "columnDefs": [ {
      "targets": 0,
      "searchable": false
    } ]
} );

这是因为我想在一个js文件中定义一个默认初始化,我在需要dataTables的页面中包含了这个文件,但仍然可以修改一些选项,以便能够自定义每页的一些内容。

我能看到的一种可能的方法是:

将数据表的初始化替换为:

var mytable;
function initialiseSpecificDatatable(options) {
    mytable = $('#example').dataTable(options);
    return mytable; // Return table so locally you can use it
}

然后,无论您在哪里指定/更改选项

var table = initialiseSpecificDatatable({
    "columnDefs": [{
        "targets": 0,
        "searchable": false
    }]
});

如果要重新创建表,您可能需要将.destroy()添加到创建函数中。要做到这一点,只需添加:

try {
    $(mytable).DataTable().destroy(); //Note capital D to return a DataTable API (See API docs)
} catch (e) {
    // Fails if the table was not a DataTable at the time
}

initialiseSpecificDatatable功能开始时。

这是我在设置默认值时发现的,并将用作我的问题的解决方案。(检查用于合并对象内容的jquery扩展的使用情况)

// Disable search and ordering by default
$.extend( $.fn.dataTable.defaults, {
    searching: false,
    ordering:  false
} );
// For this specific table we are going to enable searching except on first (0th) column
// (ordering is still disabled)
$('#example').DataTable( {
    searching: true,
    columnDefs: [{
        "targets": 0,
        "searchable": false
    }]
} );

感谢