Grid Filter Plugin – Backend Code (in PHP)

by Shea Frederick on April 18th, 2008

The code used for this example is available by itself, or as one of the examples in the ExtJS API download. This code is provided with the intent of showing how to build a filtered query from the Grid Filter Plugin in PHP, therefore it does NOT contain any security precautions. Please modify this script before use in a production environment.

The Plugin

If your not familiar with the Grid Filter plugin, I would suggest checking out the ExtJS User Extension and Plugins forum thread on this plugin. ExtJS has decided to include my PHP script in the example code released with their 2.1 API download, which I think is a great idea, since the question of how exactly to make this grid filtering work seems to come up very often.

The General Concept

There are five data types that can be filtered, and each of them has their own specific options.

  • string
  • list
  • boolean
  • numeric
  • date

All the info needed to filter a column is passed by the filter grid plugin as a single 'filter' post variable that essentially is a multi-dimentional array.

Each filtered column passes three values, and in some cases a fourth.

  • type
  • value
  • field
  • comparison (only for date or numeric - ne, eq, gt, lt)

This ends up making the post variable look something like this - with two filters.

	 filter[0][data][type] : list
	filter[0][data][value] : small,medium
	      filter[0][field] : size
	 filter[1][data][type] : boolean
	filter[1][data][value] : true
	      filter[1][field] : visible

The predictable format of post variables makes it easy to code the server side PHP for this.

The Code

This simple bit of code is all we need on the server side to have a fully filterable set of data.

See it in action here (switch 'Local Filtering' off)

UPDATE - 5/28 : Added support for 'ne' (not equals) on numeric and date fields.

9 Comments
  1. Awesome. This saved me a ton of time in having to write a custom server-side piece for filtering. Thank you!!!

  2. damian permalink

    soy muy nuevo en el mundo extjs, agradeceria un ejemplo completo y funcional para descargar, con sql y todo lo relacionado a las bases de datos..

  3. shafi permalink

    Hi,

    That is a nice work indeed.

    I am trying to replicate the same thing which is given in your demo. I setup almost all the things except grid-filter.php.

    I could not find it as part of “http://www.vinylfox.com/extjs/examples/grid-filter.zip”. Could you show us the grid-filter.php code as well.

    Thanks,
    Shafi

  4. Good plugin!

  5. Excellent !

    Thanks for that !

  6. Florian permalink

    Hi,

    Unfortunately the action page link is broken. Could you
    please fix it?

    thx
    Florian

  7. SunWuKung permalink

    Great stuff.
    My suggestion is to include the comparison ‘IN’ as well – which could take a list and generate an IN sql statement from it:
    AND somefield IN (‘asdf’,'qwer’)

    This would enable filters a’la MS Excel 2007 where you can tick several values in the filter. I think it would be useful even if the current extjs filter does not support such interface.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS