How to Filter fewer columns
How to Filter fewer columns
This is based on the page and instructions at http://www.datatables.net/release-datatables/examples/api/multi_filter_select.html
I have successfully created a table using the code exactly as was provided. The problem I am having is that there is a dropdown for every column. I would like to have a dropdown filter for only a couple of the columns. For example using a table of addresses with the columns Name, address, City, State, Zip. I would like to have the dropdown filters only for the columns City, State and Zip. Is this possible and how would I implement it?
This may have been posted but I have not been able to find the answer, probably because I am not using the right syntax in my searches.
Replies
Corrected link.
I do this by setting a class on the columns that I want filter. Use the 'dt-filter' selector in the InitComplete function (and where ever you refresh the selectors).
Note that I changed my select boxes to allow mulitple values.
later on:
@ThomD Thank you for your response. It seems to me that someone who has customized the datatable would fully understand your response. I suppose I should have stated that I am not at all familiar with the datatable customization, nor am I a hard-core javascript programmer. As a result I lack the knowledge to fully appreciate your response.
Just as a wild guess that your change (columns: [ { data: ...) would be inserted on the line preceding initComplete.
Let me be more specific in my question. Here is the unmodified JS code from the example page in the link http://www.datatables.net/release-datatables/examples/api/multi_filter_select.html :
I have a table with the columns (Name, address, City, State, Zip).
With no modifications, this code works perfectly with my table. And it places a drop down filter at the bottom of every column (Name, address, City, State, Zip). Which is the very useful function I was looking for.
But I don't need to filter the name Bill Smith or the address 123 anywhere dr. What I would like to know is where I would make changes to put a drop down filter on only two columns ( City and State).
Or if someone could tell me what this method of customization is called i can do a proper search and find it myself.
And again, thank you for your response.
Just saying that you can also try my yadcf plugin for datatables , easy to setup. see showcase
Greg, show us your code where you initialize your datatable. Compare my line 2 with your line 4.
that .dt-filter is a jQuery selector. It determines where the filters get added. All you need a the matching class name ("dt-filter" in my case) where you define your columns.
Thanks for checking back. And, that helps a little bit more.
I use the unmodified code from the example page, as indicated above. And it works fine for whatever table.
I am using (currently) just regular table with th / tfoot with table id="example"
The above code is exactly all the code I have and it works great. (plus the header links to the js and css files.) Straight out of the box, so to speak.
I have grasped that I need to define the columns but I really don't know how. I'm guessing I need something like this to have the dropdown filters on just the City and State. If this is what I need, I still don't know where or how to put it.
You have the right idea. Add that between lines 2 and3 from the example and put a comma after the ] .
@ThomD Thank you again for your help. I did get it working and your comments helped a great deal. Sorry for taking so long to respond but I had other things to worry about until this morning.