vedmack/yadcf

Problem with order of syntax in doFilter function if trying to use table.on('search') elsewhere in script

Closed this issue · 3 comments

If someone is using the DataTable.on('search.dt') functionality elsewhere to trigger other events anytime the table is searched, there is an ordering issue that can cause problems because the search is fired before the "inuse" flag is updated.

Consider the following Example:
theTable.on('search', function(){ var fw = $('select[id^=firmwareUpdate]').val(); var apply_button = false; if(fw!="default"){ var filters = $('.yadcf-filter.inuse'); if(filters.length>0){ var filtered = []; $.each(filters, function(index,filter){ filtered.push(parseInt(filter.id.substring(filter.id.lastIndexOf('-')+1))); }); if($.inArray(1,filtered)>-1 || $.inArray(2,filtered)>-1){ apply_button = true; } } } if(apply_button) $(".allowUpdate").css("visibility","visible"); else $(".allowUpdate").css("visibility","hidden"); });

If you click the default label of a select, instead of using the clear button or an external clear all filters, the table filter is fired before the "inuse" class is removed
oTable.fnFilter("", column_number_filter); $("#yadcf-filter-" + table_selector_jq_friendly + "-" + column_number).removeClass("inuse");
If these were reversed to be
$("#yadcf-filter-" + table_selector_jq_friendly + "-" + column_number).removeClass("inuse"); oTable.fnFilter("", column_number_filter);
It would follow the same syntax as the rest of the function where the class is added/removed before table manipulation.

please provide a jsfiddle test page with the issue

@vedmack https://jsfiddle.net/ringhidb/6ae740fm/28/ uses the default YADCF js found in this repo. If you choose any filter from any dropdown, you will see "Filters Currently In Use:" Increase. If you click the "x" to clear it, the counter decreases. If you select the "default" label (Filter in this case) it clears the filter search, but the count does not get decremented because of the order of lines 1193 and 1194 in src/jquery.dataTables.yadcf.js

https://jsfiddle.net/ringhidb/z7qs69p3/1/ uses my modified /src/jquery.dataTables.yadcf.js file, where I simply switch the order of 1193 and 1194, and the correct functionality is achieved. This also follows the syntax of your other functions in that page, where class manipulation is always done before oTable.fnFilter or refreshSelectPlugin etc.

Thanks,
Dan

#662 Pull Request Created for simplicity if result is acceptable