Today, i want to share about customizing one of DataTables feature. The feature that i want to change the behavior is filter function. Before we start with the code, you have to read the DataTables description on their website here.
DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, based upon the foundations of progressive enhancement, and will add advanced interaction controls to any HTML table.
You can read the all function on DataTables official website. Here, i want to change the behavior of the filter function Custom DataTables Filter Enter Event. The original filter function is based on keyup, search, input, paste, cut event, so if you whether type, cut, paste, input or search to the search box DataTables library will automatically call _fnFeatureHtmlFilter(settings) function.
The original function is look like:
[js]
/**
* Generate the node required for filtering text
* @returns {node} Filter control element
* @param {object} oSettings dataTables settings object
* @memberof DataTable#oApi
*/
function _fnFeatureHtmlFilter ( settings )
{
var classes = settings.oClasses;
var tableId = settings.sTableId;
var language = settings.oLanguage;
var previousSearch = settings.oPreviousSearch;
var features = settings.aanFeatures;
var input = ‘‘;
var str = language.sSearch;
str = str.match(/_INPUT_/) ?
str.replace(‘_INPUT_’, input) :
str+input;
var filter = $(‘
‘, {‘id’: ! features.f ? tableId+’_filter’ : null,
‘class’: classes.sFilter
} )
.append( $(‘‘ ).append( str ) );
var searchFn = function() {
/* Update all other filter input elements for the new display */
var n = features.f;
var val = !this.value ? “” : this.value; // mental IE8 fix š
/* Now do the filter */
if ( val != previousSearch.sSearch ) {
_fnFilterComplete( settings, {
“sSearch”: val,
“bRegex”: previousSearch.bRegex,
“bSmart”: previousSearch.bSmart ,
“bCaseInsensitive”: previousSearch.bCaseInsensitive
} );
// Need to redraw, without resorting
settings._iDisplayStart = 0;
_fnDraw( settings );
}
};
var searchDelay = settings.searchDelay !== null ?
settings.searchDelay :
_fnDataSource( settings ) === ‘ssp’ ?
400 :
0;
var jqFilter = $(‘input’, filter)
.val( previousSearch.sSearch )
.attr( ‘placeholder’, language.sSearchPlaceholder )
.bind(
‘keyup.DT search.DT input.DT paste.DT cut.DT’,
searchDelay ?
_fnThrottle( searchFn, searchDelay ) :
searchFn
)
.bind( ‘keypress.DT’, function(e) {
/* Prevent form submission */
if ( e.keyCode == 13 ) {
return false;
}
} )
.attr(‘aria-controls’, tableId);
// Update the input elements whenever the table is filtered
$(settings.nTable).on( ‘search.dt.DT’, function ( ev, s ) {
if ( settings === s ) {
// IE9 throws an ‘unknown error’ if document.activeElement is used
// inside an iframe or frame…
try {
if ( jqFilter[0] !== document.activeElement ) {
jqFilter.val( previousSearch.sSearch );
}
}
catch ( e ) {}
}
} );
return filter[0];
}
[/js]
and the edited function is look like this:
[js]
/**
* edited filter using enter only
* Generate the node required for filtering text
* @returns {node} Filter control element
* @param {object} oSettings dataTables settings object
* @memberof DataTable#oApi
*/
function _fnFeatureHtmlFilter ( settings )
{
var classes = settings.oClasses;
var tableId = settings.sTableId;
var language = settings.oLanguage;
var previousSearch = settings.oPreviousSearch;
var features = settings.aanFeatures;
var input = ‘‘;
var str = language.sSearch;
str = str.match(/_INPUT_/) ?
str.replace(‘_INPUT_’, input) :
str+input;
var filter = $(‘
‘, {‘id’: ! features.f ? tableId+’_filter’ : null,
‘class’: classes.sFilter
} )
.append( $(‘‘ ).append( str ) );
var searchDelay = settings.searchDelay !== null ?
settings.searchDelay :
_fnDataSource( settings ) === ‘ssp’ ?
400 :
0;
var jqFilter = $(‘input’, filter)
.val( previousSearch.sSearch )
.attr( ‘placeholder’, language.sSearchPlaceholder )
.bind(
‘keypress.DT’,function(e) {
/* Prevent form submission */
if ( e.keyCode == 13 ) {
/* Update all other filter input elements for the new display */
var n = features.f;
var val = !this.value ? “” : this.value; // mental IE8 fix š
/* Now do the filter */
if ( val != previousSearch.sSearch ) {
_fnFilterComplete( settings, {
“sSearch”: val,
“bRegex”: previousSearch.bRegex,
“bSmart”: previousSearch.bSmart ,
“bCaseInsensitive”: previousSearch.bCaseInsensitive
} );
// Need to redraw, without resorting
settings._iDisplayStart = 0;
_fnDraw( settings );
}
}
}
)
.attr(‘aria-controls’, tableId);
// Update the input elements whenever the table is filtered
$(settings.nTable).on( ‘search.dt.DT’, function ( ev, s ) {
if ( settings === s ) {
// IE9 throws an ‘unknown error’ if document.activeElement is used
// inside an iframe or frame…
try {
if ( jqFilter[0] !== document.activeElement ) {
jqFilter.val( previousSearch.sSearch );
}
}
catch ( e ) {}
}
} );
return filter[0];
}
[/js]
The major different is on
jqFilter variable bind methods, i replace keyup.DT search.DT input.DT paste.DT cut.DT with keypress.DT, and give it condition if keyCode == 13 (Enter Button pressed).
Thats all about Custom DataTables Filter Enter Event. hope this can help you.
you can find the demo here: Demo DataTable Filter
Leave a Reply