Call def with arguments from html view

53 views
Skip to first unread message

Rick

unread,
Jul 15, 2023, 1:37:16 AMJul 15
to web2py-users
How to call a def with request.vars arguments from an onclick-event in a view file? What is wrong with the code?

Thanks in advance for help!



Controller:

def insnext():
    nodename=request.vars['nodename']
    nrowid=request.vars['nrowid']
    db.next_list.update_or_insert(**db.next_list._filter_fields({ "node" : nodename , "next_node":nrowid }))

def delnext():
    nextrowid=request.vars['value']
    db(db.next_list.id==nextrowid).delete()


View:

<button onclick="jQuery.ajax('{{=URL('delnext', vars=dict(value=str(nextrowid)))}}');ch{{=nrowid}}();">delete</button>

                              
<button onclick="jQuery.ajax('{{=URL('insnext', vars=dict(value=str(nextrowid)))}}');window.location.reload();">insert {{=nrow['name']}}  {{=nrowid}}</button> 

Rick

unread,
Jul 17, 2023, 9:07:07 AMJul 17
to web2py-users
I tried a work-around by making a module-file, but it doesn't work. Thanks in advance for ideas!

Model:

db.define_table('next_list',

   Field('node'),               

   Field('next_node'))



Modules:

#!/usr/bin/env python

# -*- coding: utf-8 -*-

from gluon import *


class NextClass:

    nodename=str()

    nrowid=str()

        

    def insnext(self)#, nodename, nrowid):

        db = current.db

        db.next_list.update_or_insert(**db.next_list._filter_fields({ "node" : str(self.nodename) , "next_node": str(self.nrowid) }))




View:

{{nextclass = local_import('nextclass')}}


    {{nrows = db(db.node).select()}}

    {{for nrow in nrows:}}

                    {{count=0}}

                    {{nextdict=dict()}}

                    {{nrowid = nrow['id']}}

                    {{nextdict[count]=nextclass.NextClass()}}

                    {{nextdict[count].nodename=str(node.name)}}

                    {{nextdict[count].nrowid=str(nrowid)}}


                            {{nextdictins=nextdict[count].insnext}}

                            <button style="display:block;" id="unc{{=nrowid}}" onclick="ajax('{{=URL('default', 'nextdictins')}}');window.location.reload();">INSERT</button> 



Reply all
Reply to author
Forward
Message has been deleted
0 new messages