Help with cascading drop down list

159 views
Skip to first unread message

Matthew J Watts

unread,
Feb 8, 2018, 12:55:25 PM2/8/18
to web...@googlegroups.com
Hi community

I'm having a few problems trying to understand how create a cascading drop down list. I've followed this recipe -


i've managed to adapt it slightly, but i'm having trouble trying to add more tables to the cascade/sequence 

It is quite difficult for me (a beginner) to suss out the logic behind it

So at the moment i have the code below, what i'm trying to do is return a list of values from  the "tax_class" table, based  on a selection from the "tax_phylum" table( based on sececting an entry  from the kingdom table)

model

db.define_table('tax_kingdom',
Field('name'))

db.define_table('tax_phylum',
Field('name', 'string'),
Field('kingdom_id'))
db.tax_phylum.kingdom_id.requires = IS_IN_DB(db, db.tax_kingdom.id, '%(name)s')

db.define_table('tax_class',
Field('name', 'string'),
Field('phylum_id'))
db.tax_class.phylum_id.requires = IS_IN_DB(db, db.tax_phylum.id, '%(name)s')

Controller

def index():
kingdoms = db().select(db.tax_kingdom.ALL)

if request.vars.kingdom_name:
phylum_select = db(db.tax_phylum.id == request.vars.kingdom_name).select(db.tax_phylum.ALL)
else:
phylum_select = db(db.tax_phylum.id == 1).select(db.tax_phylum.ALL)
return dict(kingdoms=kingdoms, phylum_select=phylum_select)


def phylum():
phylums = db(db.tax_phylum.kingdom_id == request.vars.kingdom_name).select(db.tax_phylum.ALL)
result = ""
for p in phylums:
result += "<option value='" + str(p.id) + "'>" + p.name + "</option>"
return XML(result)


view


{{extend 'layout.html'}}

<form enctype="multipart/form-data" action="{{URL()}}" method="post">
<select name='kingdom_name'
onchange="jQuery('#kingdom_name').empty();
ajax('phylum', ['kingdom_name'], 'phylum_name');">
{{for kingdom in kingdoms:}}
<option value="{{=kingdom.id}}"
{{=" selected='selected'" if str(kingdom.id)==request.vars.kingdom_name else ""}}>
{{=kingdom.name}}
</option>
{{pass}}
</select>


<select id='phylum_name' name='phylum_name' >
<!-- loop through the index function i -->
{{for phylum in phylum_select:}}
<option value="{{=phylum.id}}"
{{=XML(" selected='selected'") if str(phylum.id)==request.vars.phylum_name else ""}}>
{{=phylum.name}}</option>
{{pass}}
</select>


</form>

Dave S

unread,
Feb 8, 2018, 2:56:19 PM2/8/18
to web2py-users


On Thursday, February 8, 2018 at 9:55:25 AM UTC-8, Matthew J Watts wrote:
Hi community

I'm having a few problems trying to understand how create a cascading drop down list. I've followed this recipe -


i've managed to adapt it slightly, but i'm having trouble trying to add more tables to the cascade/sequence 

I'm not much of a menu person (I make use of ordinary anchor links for the most part), but that example was probably written when BootStrap2 was the css/javascript in use.  Currently,  web2py ships with BootStrap3, which doesn't support more than 1 level of submenus.  If you use stupid.css (as in applications/example), you might not have that limitation, but I can't show you what you want.


It is quite difficult for me (a beginner) to suss out the logic behind it


Menus typically control what is shown by using javascript to manipulate CSS.  Simple menus may be CSS only, but then they'd be statically defined. 

Good luck!

/dps

 

Matthew J Watts

unread,
Feb 9, 2018, 6:05:32 AM2/9/18
to web2py-users
Thanks for your reponse David!

Matthew J Watts

unread,
Feb 12, 2018, 12:56:59 PM2/12/18
to web2py-users
Can anyone point me in the right direction with this, any good tutorial out there on how i can integrate java and web2py? 

Massimo Di Pierro

unread,
Feb 24, 2018, 9:27:52 PM2/24/18
to web2py-users
what do you want to do exctely? call a Java function from web2py?

Matthew J Watts

unread,
Mar 3, 2018, 6:54:24 AM3/3/18
to web...@googlegroups.com
Ciao Massimo

Thanks for getting back to me. Where do i start! As a beginner i'm struggling to understand how i go developing the dependent /cascading drop down list. 

There seems to be several concepts i need to learn to be able build this - i could do with a few pointers - what should i study ? Should i put my efforts into learn java ,Jquery and ajax, then things will get clearer?  i also want to incorporate data visualization into the app, so will think about d3 too

Sorry if i'm not making sense!

Grazie

Matt



--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/hLmRiU-dhvM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Val K

unread,
Mar 3, 2018, 9:43:54 AM3/3/18
to web2py-users
just for clarity:
JQuery is a Javascript library. Javascript doesn't have any relation to Java. Web2py also has nothing to do with Java. There is a misunderstanding when you talk about Java.
And yes, to do what you need, you should have understanding javascript. JQuery is quite enough for a simple application with some ajax tricks like yours. If you want to develop  a complex application with a rich UI, you have to study javascript much deeper, after that you won't have any questions about which library/framework  you'd better use (D3, Vue, Angular, React, Backbone, Knockout,  Ember ...+tons of others)
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Matthew J Watts

unread,
Mar 4, 2018, 7:18:00 AM3/4/18
to web...@googlegroups.com
Ok thanks for your tips Val, i'll focus my efforts on learning basic java script then and libraries

Cheers

Matt

To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages