\$\begingroup\$
\$\endgroup\$
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": {
"sLengthMenu": "_MENU_ records per page"
}
});
}
});
NOTE:
columnFilter
is an extension to dataTable
.
200_success
146k22 gold badges190 silver badges479 bronze badges
1 Answer 1
\$\begingroup\$
\$\endgroup\$
2
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": {
"sLengthMenu": "_MENU_ records per page"
}
});
if (target.hasClass('trans')) {
table.columnFilter({
sPlaceHolder: "head:before",
aoColumns: [
null,
{ type: "text" },
{ type: "text" },
{ type: "date-range" },
{ type: "text" },
{ type: "text" },
null,
null
]
});
}
});
answered Apr 22, 2014 at 8:12
-
\$\begingroup\$ Neat! I didn't know chaining function calls wasn't necessary. Now I know. Cheers! \$\endgroup\$Giraldi– Giraldi2014年04月22日 09:01:59 +00:00Commented Apr 22, 2014 at 9:01
-
1\$\begingroup\$ @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. \$\endgroup\$Flambino– Flambino2014年04月22日 11:18:24 +00:00Commented Apr 22, 2014 at 11:18
Explore related questions
See similar questions with these tags.
default