I am using Datatables 1.10 and YADCF 0.8.9.beta.24.
My filter inputs are shown in a dropdown menu inside each table head th
.
I want to show/hide an active-filter-icon in the th
where a filter is applied, but I don't know how to react properly to any change / selecting / remove event with the different YADCF filter types, specially with the YADCF clear button.
(I'm using select2
, text
, range_date
and auto_complete
).
What I tried:
$('#my-table thead').on('change', function(e) {
if( $(e.target).val() ) {
$(e.target).closest('th').addClass('filtered_col');
} else {
$(e.target).closest('th').removeClass('filtered_col');
}
});
//Change event triggers when selecting with 'select2'
//and when removing items with Select2 remove button,
//works also selecting with 'auto_complete' filters
//but not whith any YADCF clear button
//or when selecting with type 'text' or 'range_date'.
I tried with specific event hanlers for Select2 filters ('selecting' and 'removed'), but those are not fired when using the YADCF clear button.
I also tried:
oTable.on( 'search.dt', function() {
$(this).find('th').each(function() {
if ( $(this).find('.select2-search-choice').length || $(this).find('.inuse').length ) {
$(this).addClass('filtered_col');
} else {
$(this).removeClass('filtered_col');
}
});
});
//It works selecting with 'auto_complete' and with 'text' types.
//and with respective YADCF Clear button.
//also selecting with 'select2' type and clearing with Select2 clear button,
//but NOT clearing select2 with YADCF clear button
//'range_date' is not being targeted here.
So, how could I react to active filters to show and hide an active icon when the user filters each column?
EDIT: As suggested in @Daniel's Answer (thank you :), I tried to listen to DOMSubtreeModified
:
$('#main_container').find('.yadcf-filter-wrapper').bind('DOMSubtreeModified', function(e) {
if( $(e.target).closest('th').find('.select2-search-choice').length || $(e.target).closest('th').find('.inuse').length ) {
$(e.target).closest('th').addClass('filtered_col');
} else {
$(e.target).closest('th').removeClass('filtered_col');
}
});
//Event is only firing with 'select2' filtering and clearing
//No other filter types are firing this event, although one CSS class is being added in 'text' filters.
Soo, I tried now to modify jquery's addClass
/ removeClass
, to make them trigger some events (as suggested in this answer):
(function(){
var originalAddClass = jQuery.fn.addClass;
var originalRmvClass = jQuery.fn.removeClass;
jQuery.fn.addClass = function(){
// Execute the original method.
var result = originalAddClass.apply( this, arguments );
// trigger a custom event
jQuery(this).trigger('cssClassAdded');
// return the original result
return result;
}
jQuery.fn.removeClass = function(){
var result = originalRmvClass.apply( this, arguments );
jQuery(this).trigger('cssClassRmvd');
return result;
}
})();
// And then..
$(function() {
$('.yadcf-filter-wrapper').on('cssClassAdded', function(){
if( $(this).closest('th').find('.select2-search-choice').length || $(this).closest('th').find('.inuse').length ) {
$(this).closest('th').find('.fa-filter').css('display', 'block');
}
});
$('.yadcf-filter-wrapper').on('cssClassRmvd', function(){
if( !$(this).closest('th').find('.select2-search-choice').length && !$(this).closest('th').find('.inuse').length ) {
$(this).closest('th').find('.fa-filter').css('display', 'none');
}
});
});
//This is the best approach, since is working with 'text', 'select2' and 'auto_complete', with both filtering and clearing actions.
//'range_date' is the only one I still need to cover.
As said, with this last example I could solve the problem in all filter types but 'range_date'. The only idea I have now is, as suggested by @Daniel, to modify YADCF to introduce some class in this case (for manteinance reasons I'd like to avoid this).
I expected that 'inuse' class would be added as well in range_date
filters. It would do the trick, with my last approach. Is YADCF so desiged, to only introduce this class in some filter types and not in all of them? Maybe I am missing something?
I still would appreciate any better solution to solve this, specially for 'range_date', which I couldn't solve at all.
Thanks