drop down menu redirection or response menu with form?

65 views
Skip to first unread message

aetag...@gmail.com

unread,
Dec 28, 2015, 11:28:49 AM12/28/15
to web2py-users
Is it possible to implement something equivalent to a drop down menu within a form wizard?
I want to display a menu which exists on each page of the form, so that the user can select which step to go to, they can select the step in the menu and it redirects them to that step.
The problem is that if the user fills out the form in step 1, then they go to another step and return the that previous step the inputted data is gone. I can't find a way around this.

I also only want to display this drop down menu only on the form page, not in the navbar with the other menu items but I can't figure out how to do  it or find anything similar on the web.

Also, the drop-down functionality doesn't seem to work either when I try implementing it, the submenu just shows up in a horizontal list underneath "Go to page", rather than a menu that pops up upon clicking.

menu.py :

response.menu = [
##Regular menu here
]

             
DEVELOPMENT_MENU = True

def menu2():
    menu2= [
        (T('Go to page'), False, '#', [
            (T('General Information'), False, URL('default', 'myform/0')),
            (T('DAY1'), False, URL('default', 'myform/1')),
            (T('DAY2'), False, URL('default', 'myform/2')),
            (T('DAY3'), False, URL('default', 'myform/3')),
              ])
        ]


views/layout.html:

          {{if response.menu:}}
          {{=MENU(response.menu, _class='nav navbar-nav',li_class='dropdown',ul_class='dropdown-menu')}}
          {{pass}}



views/default/myform.html:

<h1>Day: {{=d}}</h1>
{{=form}}
{{=menu2}}

Anthony

unread,
Dec 28, 2015, 11:38:33 AM12/28/15
to web2py-users
You might consider keeping all the wizard steps in a single HTML page, and just use Javascript to show/hide the different steps. That way, data entered in one step will be retained even as you skip back and forth to other steps.

Anthony

aetag...@gmail.com

unread,
Dec 28, 2015, 12:17:17 PM12/28/15
to web2py-users
I've been trying and failing with trying to get that to work with the form wizard, it works without it.
The conditional fields just don't work at all, the form proceeds as normal showing every field in every step. I don't know if I am just placing things in the wrong place or what.

 step = int(request.args(0) or 0)
    if not step in STEPS: redirect(URL(args=0))
    fields = STEPS[step]
    if step == 0:
        d = 'General Information'
        session.myform = {}
    if step == 1:
        d = 'Day 1'
    if step == 2:
        d = 'Day 2'
    if step == 3:
        d = 'Day 3'
    if isinstance(fields,tuple):
        db.myform.field2.show_if = (db.myform.field1==True)
        form = SQLFORM.factory(*[f for f in db.myform if f.name in fields])
       
        if form.accepts(request,session):
            session.myform.update(form.vars)
            redirect(URL(args=step+1))
    else:
        db.myform.insert(**session.myform)
        session.flash = T('form completed')
        redirect(fields)
    return dict(form=form,step=step, d=d)                                                                                                 

And then with and without numerous variations:
<script>
jQuery(document).ready(function(){
   if(jQuery('#myform_field1').prop('checked'))
        jQuery('#myform_field2__row').show();
   else jQuery('#myform_field2__row').hide();
   jQuery('#myform_field1').change(function(){
        if(jQuery('#myform_field1').prop('checked'))
            jQuery('#myform_field2__row').show();
        else jQuery('#myform_field2__row').hide();});
});
</script>

It does absolutely nothing no matter how I try implementing it with the form wizard.

Anthony

unread,
Dec 28, 2015, 12:39:18 PM12/28/15
to web2py-users
Based on your code, it looks like you are making separate submissions to web2py for each step (encoding the step in the first URL arg) -- in other words, you are loading/submitting a separate page for each step. I was suggesting you handle the entire form via Javascript on a single page, with no submissions until all the steps have been filled.

Anthony

aetag...@gmail.com

unread,
Dec 28, 2015, 12:56:04 PM12/28/15
to web2py-users
Doesn't that defeat the purpose of using a form wizard, since I can use jquery effectively without it all on the same page?

Or do you mean that the form wizard helps with shortening code with this because instead of hiding/showing fields one-by-one, it can somehow allow me to do this using the wizard steps as blocks with jquery??

I have looked into jquery and did some excercises on codecadamy, and I think I understand it alright..I just don't know how to incorporate it with web2py code.

Can you give me some kind of example please?

Anthony

unread,
Dec 28, 2015, 1:06:58 PM12/28/15
to web2py-users
On Monday, December 28, 2015 at 12:56:04 PM UTC-5, aetag...@gmail.com wrote:
Doesn't that defeat the purpose of using a form wizard, since I can use jquery effectively without it all on the same page?

I suppose it depends on why you are using a form wizard. Is the purpose of the wizard to break up a larger form into chunks to make the user experience better? If so, I see no reason not to keep it all on one page. The only reason to break it up into multiple pages is if you need to submit each page for server-side validation -- but even then, you could make the interim submissions via Ajax and still keep all the form code on a single page.
 
I have looked into jquery and did some excercises on codecadamy, and I think I understand it alright..I just don't know how to incorporate it with web2py code.

My previous suggestion was to use something like http://www.jquery-steps.com/Examples#advanced-form. Nothing changes from the web2py perspective -- it's just a single form on a single page that gets submitted once when everything is complete. Regarding handling the "wizard" aspects of the form, you would have to follow the jQuery Steps documentation, as that is not web2py specific.

Anthony

aetag...@gmail.com

unread,
Dec 28, 2015, 1:44:47 PM12/28/15
to web2py-users
I want to break up the form into large chunks to make it easier for the user, yes.

I have 5 steps defined containing the fields, and then changed the code to the following so that the entire form is displayed on the page with the following:
STEPS = {0: ('fields1', 'fields2', 'fields3'),
                1: ('fields3', 'fields5', 'fields6'),
                ....#and so on

    record = db.myform(request.args(0))
    form =SQLFORM(db.myform, record)
    if form.accepts(request,session):
        response.flash='Thanks,form submitted'
        redirect(URL'(myformlist'))
    else:
       # db.myform.insert(**session.myform)

        session.flash = T('form completed')
        #redirect(fields)
    return dict(form=form) 

Now the jquery works again, but I just want to make sure I am understanding you and going about it the right way.

Can I use the steps I have defined with the form wizard, instead of having to do hundreds of jquery commands with individual fields, using the jQuery steps implementation?

Or do I use only the jQuery steps documentation without the web2py form wizard steps, and make steps only using that documentation.

I hope the way I am wording this makes sense, it sounds kind of confusing. I think I'm making this more confusing than it needs to be, sorry but I have been stuck on this for a while.
Reply all
Reply to author
Forward
0 new messages