SQLFORM.grid custom labels in edit form for each row

251 views
Skip to first unread message

brushek

unread,
Dec 16, 2011, 5:10:53 PM12/16/11
to web2py-users
Hello,

I would like to have images instead of text (or text AND image) in
each label for row, when I click edit in form generated by
SQLFORM.grid. How to do this in easiest way ?

Regards
brushek

Anthony

unread,
Dec 16, 2011, 5:32:55 PM12/16/11
to web...@googlegroups.com
In your table definition, you can specify a 'label' argument for each field, and I think that can be whatever you want (e.g., an IMG tag, etc.).

db.define_table('mytable', Field('myfield', label=IMG(_src=URL('static','img/myimage.png'))))

Haven't tried it, but hopefully should work.

Anthony

brushek

unread,
Dec 16, 2011, 5:50:26 PM12/16/11
to web2py-users
Thank You, Anthony. It was... so easy!!! I was looking for some very
strange and wierd solutions, big + for You :).

regards
brushek

brushek

unread,
Dec 17, 2011, 10:32:36 AM12/17/11
to web2py-users
But...

LABEL have some bug:

http://code.google.com/p/web2py/issues/detail?id=566

On 16 Gru, 23:32, Anthony <abasta...@gmail.com> wrote:

Anthony

unread,
Dec 17, 2011, 10:57:20 AM12/17/11
to web...@googlegroups.com
The bug isn't with LABEL, it's with SQLFORM.search_menu, which tries to translate field.label without first converting to a string in case the label is an HTML helper.

Should be an easy fix, but for now, you can do:

label=str(IMG(...))

or

label=IMG(...).xml()

either of which will convert the HTML helper to a string, so it won't cause an error with the translator.

Also, note that you shouldn't need the LABEL wrapper -- in SQLFORMs, the labels are automatically wrapped in a LABEL().

Anthony


On Saturday, December 17, 2011 10:32:36 AM UTC-5, brushek wrote:

brushek

unread,
Dec 17, 2011, 4:58:49 PM12/17/11
to web2py-users
> The bug isn't with LABEL, it's with SQLFORM.search_menu, which tries to
> translate field.label without first converting to a string in case the
> label is an HTML helper.
>
> Should be an easy fix, but for now, you can do:
>
> label=str(IMG(...))
>
> or
>
> label=IMG(...).xml()

None of this soulutions works :(. I mean, the app doesn't crash, but I
can see the text:

<img alt="Woda" src="/sdadmin/static/img_towary/1.png" />

in label of row, but no image, it isn't that what I want.

I tried:
Field('tw1','integer',label=str(IMG(_alt='Woda',_src=URL('static','img_towary/
1.png')))),
and:
Field('tw1','integer',label=IMG(_alt='Woda',_src=URL('static','img_towary/
1.png')).xml()),


>
> either of which will convert the HTML helper to a string, so it won't cause
> an error with the translator.
>

Indeed, but as You can see above, the output isn't image.

> Also, note that you shouldn't need the LABEL wrapper -- in SQLFORMs, the
> labels are automatically wrapped in a LABEL().
>

I need LABEL to make label with text AND image, and it works in:

/app/default/planetysurowce/edit/gk_planety/1

but not in:

/app/default/planetysurowce

where grid is computed.

Anyway, thanks Anthony, I think (as You said) it will be quick fix,
and all the stuff will work :)

regards
brushek

Anthony

unread,
Dec 17, 2011, 9:15:21 PM12/17/11
to web...@googlegroups.com
Sorry, forgot that the label will get escaped. Anyway, there is now a fix in trunk.
Reply all
Reply to author
Forward
0 new messages