Skip to main content
added 11 characters in body; edited tags; edited title
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Reduce code in JavaScript snippet DataTables search filter handler

I am writing a JavaScript feature where by I need to make a call to a third-party library (DataTablespre-1.10 DataTables) depending upon whether or not the user provides an integer as an input to the function during initialization. I am pasting here the snippet. As you can see it has duplicate code in the if/else clauses, the only difference is the method signature for *.fnFilter*.fnFilter.

How can I optimize this code further? Also I dont want to test the condition during each key-up.

/*
 * By default this is a global search box events. Global being searchable across all columns.
 * But if an int is provided as an input then it will apply the search only on that column.
 * */
_bindSearchEvents: function(onColumnIndex) {
    
    var self = this;
    
    if (Math.ceil(onColumnIndex) === Math.floor(onColumnIndex)) {
        self.settings.search.input.live('keyup', function() {
            self.widget.fnFilter($(this).val(), onColumnIndex);
        });
        
        self.settings.search.reset.live('click', function() {
            self.settings.search.input.val("");
            self.widget.fnFilter("", onColumnIndex);
        });
        
    } else {
        
        self.settings.search.input.live('keyup', function() {
            self.widget.fnFilter($(this).val());
        });
        
        self.settings.search.reset.live('click', function() {
            self.settings.search.input.val("");
            self.widget.fnFilter("");
        });
    }
},

Reduce code in JavaScript snippet

I am writing a JavaScript feature where by I need to make a call to a third-party library (DataTables) depending upon whether or not the user provides an integer as an input to the function during initialization. I am pasting here the snippet. As you can see it has duplicate code in the if/else clauses, the only difference is the method signature for *.fnFilter.

How can I optimize this code further? Also I dont want to test the condition during each key-up.

/*
 * By default this is a global search box events. Global being searchable across all columns.
 * But if an int is provided as an input then it will apply the search only on that column.
 * */
_bindSearchEvents: function(onColumnIndex) {
    
    var self = this;
    
    if (Math.ceil(onColumnIndex) === Math.floor(onColumnIndex)) {
        self.settings.search.input.live('keyup', function() {
            self.widget.fnFilter($(this).val(), onColumnIndex);
        });
        
        self.settings.search.reset.live('click', function() {
            self.settings.search.input.val("");
            self.widget.fnFilter("", onColumnIndex);
        });
        
    } else {
        
        self.settings.search.input.live('keyup', function() {
            self.widget.fnFilter($(this).val());
        });
        
        self.settings.search.reset.live('click', function() {
            self.settings.search.input.val("");
            self.widget.fnFilter("");
        });
    }
},

DataTables search filter handler

I am writing a JavaScript feature where by I need to make a call to a third-party library (pre-1.10 DataTables) depending upon whether or not the user provides an integer as an input to the function during initialization. I am pasting here the snippet. As you can see it has duplicate code in the if/else clauses, the only difference is the method signature for *.fnFilter.

How can I optimize this code further? Also I dont want to test the condition during each key-up.

/*
 * By default this is a global search box events. Global being searchable across all columns.
 * But if an int is provided as an input then it will apply the search only on that column.
 * */
_bindSearchEvents: function(onColumnIndex) {
    
    var self = this;
    
    if (Math.ceil(onColumnIndex) === Math.floor(onColumnIndex)) {
        self.settings.search.input.live('keyup', function() {
            self.widget.fnFilter($(this).val(), onColumnIndex);
        });
        
        self.settings.search.reset.live('click', function() {
            self.settings.search.input.val("");
            self.widget.fnFilter("", onColumnIndex);
        });
        
    } else {
        
        self.settings.search.input.live('keyup', function() {
            self.widget.fnFilter($(this).val());
        });
        
        self.settings.search.reset.live('click', function() {
            self.settings.search.input.val("");
            self.widget.fnFilter("");
        });
    }
},
Tweeted twitter.com/#!/StackCodeReview/status/114049231471394817
Source Link
Chantz
  • 213
  • 2
  • 7

Reduce code in JavaScript snippet

I am writing a JavaScript feature where by I need to make a call to a third-party library (DataTables) depending upon whether or not the user provides an integer as an input to the function during initialization. I am pasting here the snippet. As you can see it has duplicate code in the if/else clauses, the only difference is the method signature for *.fnFilter.

How can I optimize this code further? Also I dont want to test the condition during each key-up.

/*
 * By default this is a global search box events. Global being searchable across all columns.
 * But if an int is provided as an input then it will apply the search only on that column.
 * */
_bindSearchEvents: function(onColumnIndex) {
    
    var self = this;
    
    if (Math.ceil(onColumnIndex) === Math.floor(onColumnIndex)) {
        self.settings.search.input.live('keyup', function() {
            self.widget.fnFilter($(this).val(), onColumnIndex);
        });
        
        self.settings.search.reset.live('click', function() {
            self.settings.search.input.val("");
            self.widget.fnFilter("", onColumnIndex);
        });
        
    } else {
        
        self.settings.search.input.live('keyup', function() {
            self.widget.fnFilter($(this).val());
        });
        
        self.settings.search.reset.live('click', function() {
            self.settings.search.input.val("");
            self.widget.fnFilter("");
        });
    }
},