Filter Paginated Indexes using the Filter Component Plugin
This component is something of a fork of James Fairhurst's Filter Component (http://www.jamesfairhurst.co.uk/posts/view/cakephp_filter_component/), which is in turn a fork by Maciej Grajcarek (http://blog.uplevel.pl/index.php/2008/06/cakephp-12-filter-component/) which is ITSELF a fork from Nik Chankov's code at http://nik.chankov.net/2008/03/01/filtering-component-for-your-tables/ .
That's a lot of forks...
Filters hasOne and belongsTo relationships (I prefer selects from dropdowns, but to each their own).
- Clone from github : in your plugin directory type
git clone git://github.com/josegonzalez/filter-component.git filter
- Add as a git submodule : in your plugin directory type
git submodule add git://github.com/josegonzalez/filter-component.git filter
- Download an archive from github and extract it in
/plugins/filter
- Include the component in your controller (AppController or otherwise) var $components = array('Filter.Filter');
- Use something like the following in your index function index() { $filterOptions = $this->Filter->filterOptions; $posts = $this->paginate(null, $this->Filter->filter); $this->set(compact('filterOptions', 'posts')); }
- Setup your view correctly: Option 1: Use the helper In between the row with all the column headers and the first row of data add: form('Post', array('name')) ?> The first parameter is the model name. The second parameter is an array of fields. If you don't want to filter a particular field pass null in that spot.
Option 2: Manually create('Post', array('action' => 'index', 'id' => 'filters')); ?>
// loop through and display your datasort('Name', 'name', $filterOptions);?> | Actions |
---|---|
input('name'); ?> | Filter Reset |
Page Links
At this point, everything should theoretically work.
- Better code commenting
- Refactor datetime filtering for ranges
- Support jQuery Datepicker
- Allow the action to be configurable