tags:

views:

132

answers:

1

I want to know is The only way to make multi-select list is to have bridge table betwwen two tables??

I have tables (member,language,member_language)--> each member has one or more than one language i made a multi-select list of longuages, and i selected the more than languge it works fine.

But i wanna to make the same thing into regions table but here the member has only one region so when i select more than one region from multi-select list of regions it cause an error "value not in database!", and when i select one value from regions list it works fine.

i wanna only to have a list of regions that i can select more than one region to generate a report that satisfy the selected regions, i will not insert any selected regions into database, i will use them into my query only.

How can i do this??

my code:

database part

db.define_table("region_tmp", SQLField("name","string")) db.region_tmp.name.requires = IS_IN_DB(db,'region_tmp.name',multiple=True)

form part:

form=SQLFORM(SQLDB(None).define_table('myform',
       db.region_tmp.name,,submit_button="Generate Report")

The output of my code is multi-select list of regions when i select more than one region from regions list The form display "value not in database!" message under regions list and when i select one region from regions list it works fine and the report is generated.

Thanks In Advance

A: 

If I understand correctly, you want to display all the regions in a multi-select form, that would be used to return the selected regions without modifying the database?

You should first select all the regions from your database (I suppose you already did), then place them in a FORM. For example, if your table regions have the field name:

rows= db().select(db.regions.ALL)
components = [LI(INPUT(_name=i.name, _type="checkbox"), i.name) for i in rows]
return dict(form=FORM(INPUT(_type="submit"),
                      *components),
                      _method="post", _action="")

will return a form that can be inserted into a template. You can use something else than LI, like P or BR, you can also add a better formatting (a table, ...) but this is just the idea.

That was the first part. To extract the selected information, you can use that in your controller function:

def index():
    # Prepare the form
    rows = db().select(db.regions.ALL)
    components = [LI(INPUT(_name=i.name, _type="checkbox"), i.name) for i in rows]
    form=FORM(INPUT(_type="submit"),
              *components,
              _method="post", _action="")

    if request.post_vars.get("submit"):
        # Check the answer
        # ... more code ...
        redirect(URL(r=request, f="show", args=request.args))
    return dict(form=form)

def show():
    # ... more code ...

(you'll have to check about the condition after the if, I'm not 100% sure). index() will be called the first time to display the form, and a second time when the user has clicked on the Submit button. The second time, it should enter the condition, do whatever you need to do (retrieve the region values), and redirect the output to another page (here the function show()).

I'm just giving you the overall idea, but you should really tackle the tutorial and the FAQ of the web2py to get the handle on that framework.

RedGlyph
not exactly what i want , what i want is generate a report in csv but there is some criteria to generate the report according them one of the report criteria is regions "e.g: generate the report where members has region zamalek or 6October" so i displayed the list regions but it displayed the following message when i submit the form.Thanks
Neveen
Well, the first part above shows you how to display a form with the list of the regions, to select which ones you are interested in. Isn't that what you are trying to do? As for the message, it's hard to tell without any code or information.
RedGlyph
Thanks for all you effort on replying to my questions. i am a beginner in web2py technology. so there are some concepts missed.Thanks again.
Neveen
i fixed the problem which is select one of the regions its name has a space e.g:(select 5th settlement and 6th of October) i don't know why this may cause this problem. so do you have any solutionThanks
Neveen