On Sunday 10 May 2009 02:09:28 am Sibylle Koczian wrote:
> Hello,
>
> I've got a table for categories and another for items in those
> categories. Now I want to create a very simple form: a dropdown list for
> the category names and a grid showing the most important fields of the
> items table. The grid should always show the records belonging to the
> category chosen in the dropdown list. So I use the bizobj for the
> categories table as parent and the bizobj for the items table as child
> bizobj.
>
> I don't think the way to populate the list shown in the wiki is the
> right way for this case (or is it?). I tried simply to create the
> dropdown list like this:
>
>     dl = dabo.ui.dDropdownList(pn, DataSource="rubrik",
>                                DataField="rub_name")
>     dl.ValueMode = "String"
>
> But that doesn't work. When I open the application, I get this:
>
> Traceback (most recent call last):

>     raise ValueError(_("String must be present in the choices."))
> ValueError: String must be present in the choices.
>
> So how to do this?
>
> Thanks for pushes in the right direction,
> Sibylle

A dropdown is the simplest list Dabo has.  It requires at least one list (but 
in your case I think you will provide two list).  When I say 'list' I mean a 
python list.  The first list is "Choices".  It will contain the strings 
(maybe the description string in this case) from categories table.  If you 
use a second list ("Keys") it will contain the unique number associated with 
the choice string.  

There are several ways to create the list.  If the table is small. You can do 
something like:

def categoriesChoices(self):
        self.categories.requery() ##the bizobj for categories
        categoriesDS = self.categories.getDataSet()
        categories_Choices=['<None>']
        categoriesKeys=[0]
        for row in categoriesDS:
            descript=row['description'].strip()
            categories_Choices.append(county)
            categoriesKeys.append(row['pkid'])
        return categories_Choices,categoriesKeys

In the above list I have setup two values in advance
"<None>" and 0 (number 0).

If Dabo returns a Null (python None) it will convert the value to '<None>'.  
So this is to avoid the error of '<None>' not being in the list.

Next you use your list(s) method
available_choices, available_keys= self.categoriesChoices()

Now create the control on your form.
dl = dabo.ui.dDropdownList(pn, Choices=available_choices, 
Keys=available_keys,DataSource="rubrik",                           
DataField="fk_categories",ValueMode ='key')

In this case I'm using "ValueMode ='key'".  Because I like to use foreign keys 
(FK) to find my data.  So my tables would look similar to:

table categories

pkid (auto-increment)
description - a string

table rubrik ## will have at least

pkid (auto-increment)
fk_categories ## this is the PK from categories.


The dropdown will display the description field and return the key (or FK) for 
each selection.  If you still want to save the string value just change 
the "ValueMode".

BTW all the list controls work in a similar way.  It's best to check the test 
and demo code for the controls to see how they work.

-- 
John Fabiani
_______________________________________________
Post Messages to: Dabo-users@leafe.com
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: 
http://leafe.com/archives/byMID/200905100715.49969.jfabi...@yolo.com

Reply via email to