# Making a datatable, maybe also with column filters

In case the table has class 'trans' - run dataTable() and columnFilter(); otherwise just run dataTable().

Is it possible to not repeat the dataTable() part?

$('#dt_a').each(function() { if ($(this).hasClass('trans')) {
$(this).dataTable({ "sDom": "<'row'<'span6'<'dt_actions'>l><'span6'f>r>t<'row'<'span6'i><'span6'p>>", "sPaginationType": "bootstrap_alt", "oLanguage": { "sLengthMenu": "_MENU_ records per page" } }) .columnFilter({ sPlaceHolder: "head:before", aoColumns: [ null, { type: "text" }, { type: "text" }, { type: "date-range" }, { type: "text" }, { type: "text" }, null, null ] }); } else {$(this).dataTable({
"sDom": "<'row'<'span6'<'dt_actions'>l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
"sPaginationType": "bootstrap_alt",
"oLanguage": {
}
});
}
});


NOTE:

columnFilter is an extension to dataTable.

Use some variables. You don't need to chain function calls; it's just a neat thing you can do when appropriate, but it's not required.

$('#dt_a').each(function() { var target =$(this), // store this
table  = target.dataTable({  // and store this too
"sDom": "<'row'<'span6'<'dt_actions'>l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
"sPaginationType": "bootstrap_alt",
"oLanguage": {
}
});

if (target.hasClass('trans')) {
table.columnFilter({
aoColumns: [
null,
{ type: "text" },
{ type: "text" },
{ type: "date-range" },
{ type: "text" },
{ type: "text" },
null,
null
]
});
}
});

• Neat! I didn't know chaining function calls wasn't necessary. Now I know. Cheers! – Giraldi Apr 22 '14 at 9:01
• @gmaggio The reason chaining works, is that one function call returns an object, and then you call the next function in the chain on that object. So for instance \$(...) returns an object, and you can either call something on that object directly (chaining), or you can stick that object in a variable for later use. In other words, you can always cut a chain in 2 or more pieces. – Flambino Apr 22 '14 at 11:18