Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
cakephp component plugin for filtering paginated model and related model data
branch: master

This branch is 34 commits behind josegonzalez:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
controllers/components
models/behaviors
views/helpers
README.markdown

README.markdown

CakePHP Filter Plugin

Paginates Filtered Records

Background

This plugin is a fork of Jose Gonzalez's Filter component, which is something of a fork of James Fairhurst's Filter Component, which is in turn a fork by Maciej Grajcarek, which is ITSELF a fork from Nik Chankov's code.

That's a lot of forks...

This also contains a view helper made by Matt Curry.

This also uses a behavior adapted from work by Brenton to allow for HasAndBelongsToMany and HasMany relationships.

This works for all relationships.

Installation

  • Clone from github : in your plugin directory type

    git clone git://github.com/josegonzalez/cakephp-filter-plugin.git

  • Add as a git submodule : in your plugin directory type

    git submodule add git://github.com/josegonzalez/cakephp-filter-plugin.git

  • Download an archive from github and extract it in /plugins/filter

Usage

  • Include the component in your controller (AppController or otherwise)

    var $components = array('Filter.Filter');

  • Use something like the following in your index

    function index() {
    $this->paginate = array_merge_recursive($this->paginate, $this->Filter->paginate);
    $posts = $this->paginate();
    $this->set(compact('posts'));
    }
  • Setup your view correctly:

    • Option 1: Helper Use the helper In between the row with all the column headers and the first row of data add:

      <?php echo $filter->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

      <?php echo $form->create('Post', array('action' => 'index', 'id' => 'filters')); ?>

      // loop through and display your data
      <?php echo $paginator->sort('Name', 'name', $filter_options);?> Actions
      <?php echo $form->input('name'); ?> Filter Reset
      <?php echo $form->end(); ?>
      <?php echo $paginator->prev('<< '.('previous', true), array(), null, array('class' => 'disabled'));?> <?php echo $paginator->numbers($filter_options);?> <?php echo $paginator->next(('next', true).' >>', array(), null, array('class' =>' disabled'));?>
  • Add Behavior to model (only necessary for HABTM and HasMany):

    var $actsAs = 'Filter';

At this point, everything should theoretically work.

Initialization Tips

These different initialization options can be combined in the setup array.

  • For action(s) other than index, add a line to the controller such as this:

    $this->Filter->initialize($this, array('actions' => 'admin_index'));

  • To set it up for redirecting to the url with filters in it (which defaults to off), add a line to the controller such as this:

    $this->Filter->initialize($this, array('redirect' => true));

  • To set it up to include time in the filter, add a line to the controller such as this:

    $this->Filter->initialize($this, array('useTime' => true));

Todo

  1. Better code commenting - Done, left to help enforce the habit
  2. Support Datetime Done
  3. Support URL redirects and parsing Done
  4. Refactor datetime filtering for ranges - Mostly Done
  5. Allow the action to be configurable Done
  6. Support jQuery Datepicker Outside scope
Something went wrong with that request. Please try again.