Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Here's my scenario:

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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
            ]
        });
    }
});
share|improve this answer
    
Neat! I didn't know chaining function calls wasn't necessary. Now I know. Cheers! –  Giraldi Apr 22 '14 at 9:01
1  
@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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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