How to set a default value when using a filter

A place for users and developers of the Xataface to discuss and receive support.

How to set a default value when using a filter

Postby 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?
sworden
 
Posts: 44
Joined: Thu Feb 16, 2012 9:05 am

Re: How to set a default value when using a filter

Postby 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
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: How to set a default value when using a filter

Postby sworden » Wed Mar 28, 2012 11:33 am

Thanks for the explanation. It worked perfectly and I understand why it worked.
sworden
 
Posts: 44
Joined: Thu Feb 16, 2012 9:05 am

Re: How to set a default value when using a filter

Postby 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.
adf444
 
Posts: 3
Joined: Tue Nov 06, 2012 6:25 am

Re: How to set a default value when using a filter

Postby shannah » Tue Nov 06, 2012 9:09 am

Try changing
Code: Select all
!@$query['year']

to
Code: Select all
!isset($query['year'])
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: How to set a default value when using a filter

Postby adf444 » Tue Nov 06, 2012 2:00 pm

That did it. Thanks Steve.
adf444
 
Posts: 3
Joined: Tue Nov 06, 2012 6:25 am

Re: How to set a default value when using a filter

Postby 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
adf444
 
Posts: 3
Joined: Tue Nov 06, 2012 6:25 am


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 31 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved