Separate vocabularies in edit and list view?

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

Separate vocabularies in edit and list view?

Postby 00Davo » Tue Jan 15, 2013 6:12 pm

I have a select box used to configure a one-to-many relationship between two tables, which is populated using a valuelist like this:

Code: Select all
[Periods]
        __sql__ = "SELECT PeriodID, Title FROM period ORDER BY `StartDate` Desc, `Title` Asc"


I'd like to be able to filter out older, now-irrelevant values from the edit view, since newly added records won't need them. I tried doing this:

Code: Select all
[Periods]
        __sql__ = "SELECT PeriodID, Title FROM period WHERE `EndDate` IS NULL OR NOW() <= `EndDate` ORDER BY `StartDate` Desc, `Title` Asc"


And in the edit view it had pretty much the desired effect. However, this broke all the old records in list view; since they aren't defined in the valuelist any more, they just show up as numbers (PeriodID numbers).

Is it possible to define separate valuelists to use for editing and for display, so I can use both of the above?
00Davo
 
Posts: 55
Joined: Sat Jan 02, 2010 9:02 pm

Re: Separate vocabularies in edit and list view?

Postby shannah » Thu Jan 17, 2013 9:25 am

You could implement the valuelist in the delegate class.

http://xataface.com/wiki/valuelists.ini_file
(See the last section)

-Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Separate vocabularies in edit and list view?

Postby 00Davo » Mon Jan 21, 2013 4:11 pm

While that's a useful feature, I'm not sure how it helps in this situation. Since the valuelist__xxx() methods aren't given any arguments, there's no obvious way to alter the valuelist content dynamically based on which part of the app is active.

To reiterate what I'm trying to achieve:

  • When in list view (and probably also single-record detail view), I want the valuelist to use the unfiltered query. If the filtered query is used in list view, many older records end up displaying the related table's primary key, an unhelpful integer, instead of the record titles.
  • When in edit view, I'd like the valuelist instead to use the filtered query, at least when choosing which records are available in the select box. This relationship is to a table that has lots of older, historical records; one will almost never need to add a new record relating to the old ones, so it'd be preferable if those old ones could be hidden during editing.

However, as far as I can tell select boxes can only take one vocabulary setting in fields.ini, so they can't vary based on whether the user is viewing or editing.
00Davo
 
Posts: 55
Joined: Sat Jan 02, 2010 9:02 pm

Re: Separate vocabularies in edit and list view?

Postby shannah » Tue Jan 22, 2013 9:46 am

That's actually kind of intentional. However, if you're careful you can work around this with an if statement inside your valuelist method. E.g.
Code: Select all
function valuelist__myvalues(){
    $app = Dataface_Application::getInstance();
    $query = $app->getQuery();
    if ( $query['-table'] == 'mytable' and ($query['-action'] == 'edit' or $query['-action'] == 'new') ){
        $sql = "select * from foo where bar=10";
    }  else {
        $sql = "select * from foo";
    }
    $res = mysql_query($sql, df_db());
    ...

    ...  etc ...
}


You might also look into using the depselect widget or the lookup widget which don't use valuelists, but rather just specify the table and query parameters as part of the widget. Usually when I use these widgets, I also use a valuelist for use in view and list mode. Either that, or I make the field hidden in list and view modes and use a grafted field to show the value instead. It depends. Valuelists with lots of values (e.g. > 1000) can kill performance because they are loaded in every request. If the valuelist is going to be very large then I'll usually opt for the depselect or lookup widgets and use grafted fields for the view instead.

-Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Separate vocabularies in edit and list view?

Postby 00Davo » Wed Jan 23, 2013 6:35 pm

I did initially try using a lookup widget, but since the filtering requires some calculation it's not easy to express in Xataface-GET-parameter form.

Implementing the valuelist using a delegate-class function does work, however, and appears to function perfectly. So thanks! ^_^
00Davo
 
Posts: 55
Joined: Sat Jan 02, 2010 9:02 pm

Re: Separate vocabularies in edit and list view?

Postby shannah » Thu Jan 24, 2013 10:11 am

00Davo wrote:I did initially try using a lookup widget, but since the filtering requires some calculation it's not easy to express in Xataface-GET-parameter form.


If the query is too complex to express using GET parameters, you could always create a view and use that as the widget:table directive in the lookup widget. Usually I find that I don't need to go this far though.. Generally I can just add a grafted field or two that is set up specifically to allow my query to give me what I want - and that is enough.

But if the valuelist solution is working for you, then all power to you.

-Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 1 guest

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