DataGrid hyperlink in column

53 views
Skip to first unread message

RyanWilson

unread,
Oct 31, 2012, 5:11:39 PM10/31/12
to turbo...@googlegroups.com
Hi,


Can someone recommend a way to make to make the appearance 'Edit' button in the Action column conditional on another field?

My data table is totally different, so for example, I'd like the 'Edit' link to only show when phone number start with '323'

addressbook_grid = DataGrid(fields=[

    SortableColumn('Name', 'name'),

    SortableColumn('Surname', 'surname'),

    SortableColumn('Phone', 'phone'),

    ('Action', lambda obj:genshi.Markup('<a href="%s">Edit</a>' % url('/edit', params=dict(item_id=obj.uid))))

])



Alessandro Molina

unread,
Oct 31, 2012, 6:11:52 PM10/31/12
to turbo...@googlegroups.com
The field callback actually receives the entire row (the obj parameter
you see in the lambda) so you can return your content making decisions
related to any field of the row.
> --
> You received this message because you are subscribed to the Google Groups
> "TurboGears" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/turbogears/-/j-RfyOcCjCYJ.
> To post to this group, send email to turbo...@googlegroups.com.
> To unsubscribe from this group, send email to
> turbogears+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/turbogears?hl=en.

RyanWilson

unread,
Nov 1, 2012, 10:29:38 AM11/1/12
to turbo...@googlegroups.com
Hi Alessandro,

You comment was vague but led me in the right direction.  Here's what I came up with:

def checkPhone(obj):
   if some regex on obj.phone:
        return genshi.Markup('<a href="%s">Edit</a>' % url('/edit', params=dict(id=obj.id)))
    else:
        return ''

addressbook_grid = DataGrid(fields=[

    SortableColumn('Name', 'name'),

    SortableColumn('Surname', 'surname'),

    SortableColumn('Phone', 'phone'),

    ('Action', lambda obj: checkPhone(obj))

])


Works great.  Thanks for the help.

Best,

Ryan

RyanWilson

unread,
Nov 1, 2012, 5:45:54 PM11/1/12
to turbo...@googlegroups.com
How do I get that 'Edit' hyperlink to be formatted like a hyperlink?  Right now it's just plain text.

Thanks!

Ryan

Moritz Schlarb

unread,
Nov 2, 2012, 4:56:44 PM11/2/12
to turbo...@googlegroups.com
You could also just write:
    ('Action', checkPhone)
instead of:
    ('Action', lambda obj: checkPhone(obj))


RyanWilson

unread,
Nov 2, 2012, 5:38:10 PM11/2/12
to turbo...@googlegroups.com
Thanks.  Although I have to admit I don't understand why that works.  How does checkPhone get passed an argument?

Moritz Schlarb

unread,
Nov 3, 2012, 3:25:22 AM11/3/12
to turbo...@googlegroups.com
Well, that's one of the awesome language features of Python! Functions are "first-level objects" just like classes or instances, so you can pass them around just like variables!

The important thing is that you don't call the function there, the calling gets done somewhere in DataGrid!

Reply all
Reply to author
Forward
0 new messages