Got it!

Thanks to this post by wavydavy on another thread:
http://groups.google.com/group/turbogears/msg/79e1b381ef60dbe2?dmode=source

First, I need to make a Widget derivative class like so:

class MakeImgTag(Widget):
    params = ['field']
    template="""<img src="${field}"></img>"""

Then I need to make a generic function (not tied to any class) that
will get the value of a given "row" or database record. As follows:

def widget_getter(widget, field):
    def getter(row):
        return widget.display(field=getattr(row, field, ''))
    return getter

Then call these together in a field definition, like the following:

        pacts_field = [
            ('ID', 'id'),
            (PaginateDataGrid.Column(name='picture_url',
getter=widget_getter(MakeImgTag(), 'picture_url') )),
            ...
        ]
        return dict(
                pacts=Pact.select(),
                pacts_list=PaginateDataGrid(fields=pacts_field)]

and on the template:
        <div id="db_console">
        ${pacts_list.display(pacts)}
        </div>

So basically, for every object in the 'pacts' collection, the
'picture_url' field's value will be piped into the MakeImgTag widget
which will dress it up with the appropriate tag (<img> in this case),
then the whole nicely formatted tag is spit out to the browser which
will dutifully fetch the image and display it inside the
PaginateDataGrid.

Awesome, simply awesome.

Thanks wavydavy for the code (in the referred thread).

Will


On Aug 28, 4:11 pm, shadowfox <[EMAIL PROTECTED]> wrote:
> Hi Alberto,
>
> I've looked at various threads and I think I am close, except for a
> little detail that escapes me.
> Here's what I have:
>
> I create a Widget class derivative:
>
> class MakeImgTag(Widget):
>     params = ['picture_url']
>     template='''<img src="${picture_url}"/>'''
>
> inside my Root method, I declare the PaginateDataGrid fields as
> follows:
>
>         pacts_field = [
>             ('ID', 'id'),
>             (PaginateDataGrid.Column(name='Picture',
>                 getter=MakeImgTag(picture_url='picture_url'))),
>             ...
>
> based on this object inside model.py:
>
> class Pact(SQLObject):
>     picture_url = StringCol(length=150, varchar=True,title="Main
> Picture URL")
>     ...
>
> and on the rendered page I got the following:
>
>     <img src="picture_url">
>
> So it's obvious that I don't know how to pass the *value* of the
> object field named 'picture_url' into the PaginateDataGrid.Column()
>
> Any pointers?
>
> Will
>
> On Aug 28, 3:34 pm, Alberto Valverde <[EMAIL PROTECTED]> wrote:
>
> > On Aug 28, 2007, at 9:50 PM, shadowfox wrote:
>
> > > Hi all,
>
> > > Can anyone direct me to show a picture in a DataGrid widget, or
> > > recommend another widget to accomplish this.
>
> > > Hopefully I'm not the only one who needs this feature.
>
> > Check out this thread:http://tinyurl.com/2l34vh
>
> > Alberto


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to