OK, I was able to do it with SQLFORM.grid(), using jQuery by:
* registering a change function on the #shelves_genre field
* execute an XMLHttpRequest upon a change of #shelves_genre that updates 
the innerHTML of #shelves_shelve_items
It would be nice however if something similar (registering form-events 
onchange/onselect/...) could be achieved when e.g. creating the form, so 
that you can do it in python instead of having to add the javascript 
functions.



On Friday, November 13, 2020 at 3:53:23 PM UTC+1 Tom Clerckx wrote:

> Hi,
>
> Considering the sample code below.
> I have manually populated the books table with a number of books for 
> different genres.
> Now I can create an entry in the shelves table, using the SQLFORM.grid
> The shelve_items will be shown as a dropdown list with the books that I've 
> added
>
> What I would like is to update the contents of the shelve_items dropdown 
> list in the form, based on the selected genre in that same form.
>
> In the past I did something similar, using a SELECT() box and adding an 
> onchange=ajax(...) call to update a DIV elsewhere in the page.
>
> I was wondering however, if there is a better way to do this directly in 
> the SQLFORM.grid()
>
>
> ===========
> index.html
> ===========
> {{extend 'layout.html'}}
> {{=form}}
>
> ===========
> default.py
> ===========
>
> def index():
>     form = SQLFORM.grid(db.shelves, user_signature=False)
>     return dict(form=form)
>
> ======
> db.py
> ======
> genres = ['thriller', 'sf', 'roman', 'fantasy']
> db.define_table('books',
>       Field('name', 'string'),
>       Field('author', 'string'),
>       Field('genre', 'string', requires=IS_IN_SET(genres)),
>       format='%(name)s'
>       )
> db.define_table('shelves',
>       Field('genre', 'string', requires=IS_IN_SET(genres)),
>       Field('shelve_items', 'reference books'),
>       )
>
>

-- 
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 the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/61c5b124-cf09-4df5-aa58-dd6589bc63b8n%40googlegroups.com.

Reply via email to