简体   繁体   English

数据表设置默认选项

[英]Datatables setting default options

This is how I initialize a dataTable 这就是我初始化数据表的方式

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

Can I do something like below? 我可以做下面的事情吗?

var mytable = $('#example').dataTable();

mytable.columnDefs({
  "columnDefs": [ {
      "targets": 0,
      "searchable": false
    } ]
} );

This is because I want to define a default initialization in a js file that I include in page where I need dataTables, but still be able to modify few options to be able to customize few thing per page. 这是因为我想在我需要数据表的页面中包含的js文件中定义默认的初始化,但是仍然能够修改一些选项以能够为每页自定义少量内容。

One possible way I can see of doing this is: 我可以看到的一种可能的方式是:

Replace your initialisation of the datatable with: 将数据表的初始化替换为:

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

Then wherever you specify/change the options 然后,无论您在何处指定/更改选项

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

You may want to add a .destroy() to the creation function in case you want to recreate the table. 如果要重新创建表,可能需要向创建函数添加.destroy() To do so, simply add: 为此,只需添加:

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
}

At the start of the initialiseSpecificDatatable function. initialiseSpecificDatatable函数的开始。

This is what I found for setting defaults, and will be using as a solution to my problem. 是我发现的用于设置默认值的内容,并将被用作解决我的问题的方法。 (Check the use of jquery extend that is used to merge content of objects) (检查是否使用了用于合并对象内容的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
    }]
} );

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM