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.

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.