Is it possible to apply a certain filter to only one datatable? I have the following filter function that I am applying on document ready, I don't know if this is proper procedure, but as a side effect all dataTables will be affected by the filter. I would Like to affect only the $('#productTable'), but this selector appears to not have the desired effect.
//Filter Function in Stock
//$('#productTable').
$.fn.dataTableExt.afnFiltering.push(function(oSettings, aData, iDataIndex) {
var checked = $('#instock').is(':checked');
var qntStock = 1;
var stockCol = 3;
if (!checked) {
return true;
}
if (checked && aData[stockCol] > qntStock) {
return true;
}
return false;
});
Is it possible to apply a filter only to a particular table? How do I accomplish this?
EDIT:
dataTable initialization:
var oTable = $('#productTable').dataTable({
"aoColumnDefs": [{
"sClass": "my_class",
"aTargets": [4]
}],
"bAutoWidth": false,
"iDisplayLength": 100,
"fnDrawCallback": function() {
$("td.my_class").editable(function(value, settings)
{
return(value);
},
{
indicator : 'Save...',
tooltip : 'Click to Edit...'
}
);
}
});
With the current version of DataTables you can do this using the 'search' function. var data_table = $('#data-table'). DataTable(); var column_index = 0; data_table. columns(column_index).search('^(?:(?!-).)
Searching on individual columns can be performed using the columns().search() and column().search() methods. DataTables has a built in search algorithm referred to as "smart" searching and is designed to make searching the table data, easy to use for the end user.
You could create an array of tables to have the filter - then in your filter check if the current table is present in that array ... something like :
// setup an array of the ids of tables that should be allowed
var allowFilter = ['productTable'];
$.fn.dataTableExt.afnFiltering.push(function(oSettings, aData, iDataIndex) {
// check if current table is part of the allow list
if ( $.inArray( oSettings.nTable.getAttribute('id'), allowFilter ) == -1 )
{
// if not table should be ignored
return true;
}
var checked = $('#instock').is(':checked');
var qntStock = 1;
var stockCol = 3;
if (!checked) {
return true;
}
if (checked && aData[stockCol] > qntStock) {
return true;
}
return false;
});
you can do something like this: add a parameter to the configuration:
var oTable = $('#productTable').dataTable({
"applyFilter":true,
"aoColumnDefs": [{
"sClass": "my_class",
"aTargets": [4]
}],
"bAutoWidth": false,
"iDisplayLength": 100,
"fnDrawCallback": function() {
$("td.my_class").editable(function(value, settings)
{
return(value);
},
{
indicator : 'Save...',
tooltip : 'Click to Edit...'
}
);
}
});
and then, verify if your filter is active:
//Filter Function in Stock
//$('#productTable').
$.fn.dataTableExt.afnFiltering.push(function(oSettings, aData, iDataIndex) {
if(oSettings.applyFilter)
{
var checked = $('#instock').is(':checked');
var qntStock = 1;
var stockCol = 3;
if (!checked) {
return true;
}
if (checked && aData[stockCol] > qntStock) {
return true;
}
return false;
}
else
return true;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With