1
\$\begingroup\$

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
asked Apr 22, 2014 at 8:03
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ Neat! I didn't know chaining function calls wasn't necessary. Now I know. Cheers! \$\endgroup\$ Commented 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\$ Commented Apr 22, 2014 at 11:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.