How to set a default value when using a filter
Archived from the Xataface Users forum.
sworden — Wed Mar 28, 2012 10:17 am
This page: http://www.xataface.com/wiki/filter shows how to create a filter, which I’ve done successfully. In the example on that page (http://fueleconomydb.com/index.php?-action=list) some of the pull-down menus have default values other than “All”. How do I set a default value for my pull-down filter list?
shannah — Wed Mar 28, 2012 11:17 am
The default value of a filter reflects the current search query. I.e. this is equivalent to the problem of having a default search/filter for a field.
E.g.
Suppose you have a ‘year’ field and you want, by default, only those records where year=2008 to be shown.
My solution for this is to generally add this to the query in the beforeHandleRequest method of the application delegate class.
- Code: Select all
function beforeHandleRequest(){ $app = Dataface_Application::getInstance(); $query =& $app->getQuery(); if ( !$_POST and $query['-table'] == 'invoices' and !@$query['year'] ){ $query['year'] = '2008'; } }
Then if you had filter=1 for the year, field, you would see 2008 selected by default (and only 2008 would be shown by default.
-Steve
sworden — Wed Mar 28, 2012 11:33 am
Thanks for the explanation. It worked perfectly and I understand why it worked.
adf444 — Tue Nov 06, 2012 6:27 am
Thanks Steve. This works for me partially. It sets the default, and allows me to set my own filter, but when i try to explicitely UNFILTER, ie. set the filter to All, it won’t take. Instead, it just reverts back to the default filter.
shannah — Tue Nov 06, 2012 9:09 am
Try changing
- Code: Select all
!@$query['year']
to
- Code: Select all
!isset($query['year'])
adf444 — Tue Nov 06, 2012 2:00 pm
That did it. Thanks Steve.
adf444 — Tue Nov 06, 2012 8:26 pm
One more on this, if i may -
At the moment in our database we have zero records of the default filter value. Hence, the result list is not shown and the filter dropdown is not available.
I suspect this is not an easy fix, but I just wanted to make note of the scenario. Thanks