Extracting values of a list from the database and using them individually in a javascript array

40 views
Skip to first unread message

Cypher

unread,
Mar 21, 2017, 6:45:36 AM3/21/17
to web2py-users
Hi guys i need help, please someone out there coz I'm pulling out my hairs at this point:
I want to extract pictures from within a list extracted from the database and store them inside a JavaScript array for a slide show.

CONTROLLER:
def form1Details():
    form=db.compForm(request.args(0))
    forms=db(db.compFormPages.formName==form.id).select(db.compFormPages.ALL)
    return locals()

the images are in variable forms so i wanna display them in my view such that each picture is stored individually inside a JavaScript array for a slide show like below

VIEW
{{for pic in form:}}

<script>
function slideShow()
{
images = ["{{=URL('download', args=pic.formImage)}}", "{{=URL('download', args=pic2.formImage)}}", "{{=URL('download', args=pic3.formImage)}}"];

descriptions=['{{=pic.formTitle}}', '{{=pic2.formTitle}}', '{{=pic3.formTitle}}'];
...............
...............
...............
</script>

}

How do extract them individually to achieve the above, i could use all the help i could get, thank you.

Massimo Di Pierro

unread,
Mar 21, 2017, 2:18:21 PM3/21/17
to web2py-users
def form1Details():
    form = db.compForm(request.args(0))
    forms = db(db.compFormPages.formName==form.id).select(db.compFormPages.ALL)
    images = [{'link':URL('download',args=form.formImage), 'title':form.formTitle} for form in forms]
    return dict(images=images)

the images are in variable forms so i wanna display them in my view such that each picture is stored individually inside a JavaScript array for a slide show like below

VIEW

<script>
{{=ASSIGNJS(IMAGES=images)}}; // magic to convert python to JS object 
function slideShow() {
var images = IMAGES.map(function(i){return i.link;}); // array of links
var descriptions= IMAGES.map(function(i){return i.title;}); // array of titles
}
</script>

Cypher

unread,
Mar 24, 2017, 3:29:01 AM3/24/17
to web2py-users
gracias Massimo

Dave S

unread,
Mar 24, 2017, 2:17:26 PM3/24/17
to web2py-users


On Tuesday, March 21, 2017 at 11:18:21 AM UTC-7, Massimo Di Pierro wrote:
def form1Details():
    form = db.compForm(request.args(0))
    forms = db(db.compFormPages.formName==form.id).select(db.compFormPages.ALL)
    images = [{'link':URL('download',args=form.formImage), 'title':form.formTitle} for form in forms]
    return dict(images=images)

the images are in variable forms so i wanna display them in my view such that each picture is stored individually inside a JavaScript array for a slide show like below

VIEW

<script>
{{=ASSIGNJS(IMAGES=images)}}; // magic to convert python to JS object 
function slideShow() {
var images = IMAGES.map(function(i){return i.link;}); // array of links
var descriptions= IMAGES.map(function(i){return i.title;}); // array of titles
}
</script>


ASSIGNJS()  doesn't seem to be in the book; is it documented somewhere?

/dps

Reply all
Reply to author
Forward
0 new messages