Custom Filtering
Custom Filtering
Hey Allan, hey Community,
first of all : Great Tool ! I really like datatables and enjoyed using it ... since now ;)
First of all a little URL and talking about what i am actually doing :
http://iati.duman.com/
It will be a booking engine for our customers to search for flights which then can be booked or reservated.
If you try to visit this page - which i am currently developing - you will see a search box with some inputfields.
Please just fill out the source and destination - for example - with 'DUS'('Düsseldorf Airport') in the input field called "Von" and then "IST"("Istanbul Airport") into "Nach". Then just pick a date in the inputfield ('Abflugdatum') to determine the time when you want to travel.
I am explaining this to just make sure you get to see the datatables ;)
Then you get a datatable produced by php-processed result (Takes 18 Seconds though, because the Webservice which provides us with the data is really slow). Afterwards you realize on the left side a filtering-menu.
I did manage to get to work a filtering system which checks for active filters (Its all deployed in an own object) so i always know which filters are active and they use the filtering by regexpression.
Basic Initialization happens like that :
$('.departure').dataTable().fnFilter(search_term_final, 0, true, false);
$('.return').dataTable().fnFilter(search_term_final, 0, true, false);
and the "search_term_final" variable contains my regexpression which i did produce from choosen Airlines on the left side.
Now i have to do another way of filtering :
It´s called "Stops" in the left-side filter-menu. Basically filtering with these options should decide if you want to take a flight without doing a via-flight or maybe without doing "direct flight". So the filter should realize how many "travel-stops are contained in one flight-connection.
The information on the travel-stops is already available in every table-row - if you use the chrome or firefox dev tools you see every "tr" has an attribute which is called "num_of_flights".
So now i need to know how i can filter by an attribute of an table row. Is it hard to do ?
I did not find an example.
It should be an implementation which keeps the reg filtering of my Airlines intact (which i did deploy on the first column).
Does anyone know how to do this or maybe just give a hint ?
Thanks in advance !
Jeremy
first of all : Great Tool ! I really like datatables and enjoyed using it ... since now ;)
First of all a little URL and talking about what i am actually doing :
http://iati.duman.com/
It will be a booking engine for our customers to search for flights which then can be booked or reservated.
If you try to visit this page - which i am currently developing - you will see a search box with some inputfields.
Please just fill out the source and destination - for example - with 'DUS'('Düsseldorf Airport') in the input field called "Von" and then "IST"("Istanbul Airport") into "Nach". Then just pick a date in the inputfield ('Abflugdatum') to determine the time when you want to travel.
I am explaining this to just make sure you get to see the datatables ;)
Then you get a datatable produced by php-processed result (Takes 18 Seconds though, because the Webservice which provides us with the data is really slow). Afterwards you realize on the left side a filtering-menu.
I did manage to get to work a filtering system which checks for active filters (Its all deployed in an own object) so i always know which filters are active and they use the filtering by regexpression.
Basic Initialization happens like that :
$('.departure').dataTable().fnFilter(search_term_final, 0, true, false);
$('.return').dataTable().fnFilter(search_term_final, 0, true, false);
and the "search_term_final" variable contains my regexpression which i did produce from choosen Airlines on the left side.
Now i have to do another way of filtering :
It´s called "Stops" in the left-side filter-menu. Basically filtering with these options should decide if you want to take a flight without doing a via-flight or maybe without doing "direct flight". So the filter should realize how many "travel-stops are contained in one flight-connection.
The information on the travel-stops is already available in every table-row - if you use the chrome or firefox dev tools you see every "tr" has an attribute which is called "num_of_flights".
So now i need to know how i can filter by an attribute of an table row. Is it hard to do ?
I did not find an example.
It should be an implementation which keeps the reg filtering of my Airlines intact (which i did deploy on the first column).
Does anyone know how to do this or maybe just give a hint ?
Thanks in advance !
Jeremy
This discussion has been closed.
Replies
You can use a custom filter to do that - http://datatables.net/development/filtering#row_filters . With that you can check to see if each row matches whatever filtering condition you have.
Allan
function( oSettings, aData, iDataIndex ) {
return false;
}
);
i´ve seen this already- can´t get it to work with my example - what do oSettings, aData, iDataIndex mean ? It´s not documented anywhere :/
The example i have written (return false) <- seems even not to work because i would understand this sort of filtering like every row gets dropped ( because of the return false). But it does not work.
What am i doing wrong ?
What you really want is something like this:
[code]
$.fn.dataTableExt.afnFiltering.push(
function( settings, data, dataIndex ) {
var row = settings.aoData[ dataIndex ].nTr;
return row.getAttribute( 'num_of_flights' ) == myGlobalVariableForNumberOfFlights ?
true : false;
}
);
[/code]
So the (stupidly named, but descriptive) variable `myGlobalVariableForNumberOfFlights` contains the number of flights you want to search for. If the row's `num_of_flights` attribute matches then the function returns true and the row is allowed to show. Otherwise it returns false which removes the row from the display.
Just call fnDraw when you want to perform the filter (since it is a global filter it is applied on every draw).
Allan
At least write that you have to invoke FnDraw() to see Filtering Work :D
Another question :
$.fn.dataTableExt.afnFiltering.push() <- It means we have an array which gets filled with filtering functions. Are they beeing executed one by one ?
Yes. The filtering is cumulative. So the global and column filters in DataTables are applied first, and then the custom filters in afnFiltering . If a row is removed by any filter, then it can't be brought back in by another.
And yes, I completely agree that the documentation needs to be improved! As I say, I'm working on it when possible.
Allan
Great so i can continue my plan building an object and make "named"-filters which can be revoked and deleted and modified like i want. That should really fit it.
Thx a lot for the quick help !
Jeremy