我正在处理数据表,我遇到了这个问题。
我在一个页面(以及我的系统中)有多个表,所有这些表都有通用选项,如数据源,响应等。 但是有些表有一些独特的选项,比如bSort,aoColumns等......
所以我的问题是:我可以使用commom选项初始化所有这些选项,然后单独更改这些选项吗?
<小时/> 现在,这就是我正在做的事情:
var TabClienteFisico_01 = $('#cfisico').dataTable({
responsive: true,
bProcessing: true,
bServerSide: false,
aoColumns : [
{ "mData": "id" },
{ "mData": "nm_cliente" },
{ "mData": "cpf" },
{ "mData": "tel" },
{ "mData": "cidade" }
],
"ajax": {
"url": "data/c_fisico.json",
},
"dataType": "json"
});
var TabClienteFisico_02 = $('#cfisico2').dataTable({
responsive: true,
bProcessing: true,
bServerSide: false,
bSort: false,
aoColumns: [
{ "mData": "id" },
{ "mData": "nm_cliente" },
{ "mData": "cpf" }
],
"ajax": {
"url": "data/c_fisico.json",
},
"dataType": "json"
});
这就是我想做的事情:
var mainTab = $('.mainTab').dataTable({
responsive: true,
bProcessing: true,
bServerSide: false,
"ajax": {
"url": "data/c_fisico.json",
},
"dataType": "json"
});
var TabClienteFisico_01 = $('#cfisico1').dataTable({
aoColumns: [
{ "mData": "id" },
{ "mData": "nm_cliente" },
{ "mData": "cpf" },
{ "mData": "tel" },
{ "mData": "cidade" }
]
});
var TabClienteFisico_02 = $('#cfisico2').dataTable({
bSort: false,
aoColumns: [
{ "mData": "id" },
{ "mData": "nm_cliente" },
{ "mData": "cpf" }
]
});
在HTML中它将是:
<table id="cfisico1" class"mainTab"> ... </table>
<table id="cfisico2" class"mainTab"> ... </table>
有办法做到这一点吗?
答案 0 :(得分:1)
您可以将作为参数传递的选项对象扩展为datatable方法,例如:
var options = {
responsive: true,
bProcessing: true,
bServerSide: false
};
var opt01 = $.extend({}, options, {
aoColumns: [
{ "mData": "id" },
{ "mData": "nm_cliente" },
{ "mData": "cpf" },
{ "mData": "tel" },
{ "mData": "cidade" }
]
});
var opt02 = $.extend({}, options, {
bSort: false,
aoColumns: [
{ "mData": "id" },
{ "mData": "nm_cliente" },
{ "mData": "cpf" }
]
});
var TabClienteFisico_01 = $('#cfisico1').dataTable(opt01),
TabClienteFisico_02 = $('#cfisico2').dataTable(opt02);