OnClick Javascript Action for GridActionButton

61 views
Skip to first unread message

Kevin Keller

unread,
Feb 15, 2021, 11:37:46 AM2/15/21
to py4web
Hey Jim, 

I would need an action button in the grid to fire a javascript function instead of linking to a different url. 

Would it be easy for you to add this as a parameter in the GridActionButton Class?

I tried to do it myself, but failed.. :(

Thanks for your consideration. 



cloudHQPowered by
cloudHQ

Jim Steil

unread,
Feb 15, 2021, 12:05:16 PM2/15/21
to py4web
Hey Kevin

Can you try something like this?

<a href="javascript:function_to_call(); return false;">My Link</a>

Make sure it returns false.  So, on the url of the action button you'd specify:

url="javascript: function_to_call(); return false;"

I haven't tested it, hoping you can...

-Jim

Kevin Keller

unread,
Feb 15, 2021, 12:23:09 PM2/15/21
to Jim Steil, py4web
I get this:

VM242:1 Uncaught SyntaxError: Illegal return statement

But I think I may be able to add _onclick to the calss in grid.py..

lets see

cloudHQPowered by
cloudHQ
--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/py4web/cd60e7e9-933f-4725-9561-0fb07a971d74n%40googlegroups.com.

Kevin Keller

unread,
Feb 15, 2021, 12:29:46 PM2/15/21
to py4web
Hmm I added onclick but url seems required..

will fidlle some more

Jim Steil

unread,
Feb 15, 2021, 12:43:30 PM2/15/21
to py4web
Can you use '#' for url?

Kevin Keller

unread,
Feb 15, 2021, 2:07:48 PM2/15/21
to Jim Steil, py4web
It goes to /static/# if i use url='#' and I get a 404. 

I need to conditionally disable the url requirement I suppose. 



Here is what I did in grid.py 


    def render_action_button(

        self,

        url,

        button_text,

        icon,

        icon_size="small",

        additional_classes=None,

        additional_styles=None,

        override_classes=None,

        override_styles=None,

        message=None,

        onclick=None,

        row_id=None,

        name="grid-button",

        row=None,

        **attr,

    ):

        separator = "?"

        if row_id:

            url += "/%s" % row_id


        classes = self.param.grid_class_style.classes.get(name, "")

        styles = self.param.grid_class_style.styles.get(name, "")


        def join(items):

            return (

                " ".join(items) if isinstance(items, (list, tuple)) else " %s" % items

            )


        if override_classes:

            classes = join(override_classes)

        elif additional_classes:

            classes += join(additional_classes)

        if override_styles:

            styles = join(override_styles)

        elif additional_styles:

            styles += join(additional_styles)


        if callable(url):

            url = url(row)


        link = A(

            I(_class="fa %s" % icon),

            _href=url,

            _role="button",

            _class=classes,

            _message=messagea

            _onclick=onclick,

            _title=button_text,

            _style=styles,

            **attr,

        )


And this is the controller class init:


class GridActionButton:
    def __init__(self,
                 url,
                 text,
                 icon=None,
                 additional_classes=None,
                 message=None,
                 append_id=False,
                 onclick=None):
        self.url = url
        self.text = text
        self.icon = icon
        self.additional_classes = additional_classes
        self.message = message
        self.append_id = append_id
        self.onclick=None



cloudHQPowered by
cloudHQ

Kevin Keller

unread,
Feb 15, 2021, 2:11:20 PM2/15/21
to Jim Steil, py4web
The onclick also does not get rendered, seems my implementation does not work. 

Its not urgent, I think I can work around it. 

But I think its a great feature to have, if you ever get a minute to have a look at it. 


cloudHQPowered by
cloudHQ

Kevin Keller

unread,
Feb 15, 2021, 5:23:13 PM2/15/21
to Jim Steil, py4web

Kevin Keller

unread,
Feb 16, 2021, 12:27:33 AM2/16/21
to Jim Steil, py4web
I managed, will create a PR tomorrow

cloudHQPowered by
cloudHQ
Reply all
Reply to author
Forward
0 new messages