I could not find any documentation on how to write a widget but I took
a stab at it by reading the code.  Sorry, I am not an expert Python
programmer.  About 1 week worth of coding so far.  This grid took about
3 hours.  Mainly playing around.  Thanks for help on getattr.

I started with this
http://www.checkandshare.com/blog/?p=24


Anyway, you can paste this onto the end of widgets/grid.py

THE CODE
# configurable grid that accepts a dataset
class DataGrid2(Grid):
    css=[CSSLink(static, "grid.css")]

    def __init__(self, name='grid', config=None, dataset=None,**kw):
        self.name = name
        if config:
            self.headers = []
            for col_config in config:
                if col_config.has_key('texttype'):
                    # change the way column headers are built here
                    cell =''
                    if col_config.has_key('coltip'):
                        cell = cell + '<a title="' +
col_config['coltip'] + '">'
                    cell = cell + col_config['colhdr']
                    if col_config.has_key('coltip'):
                        cell = cell + '</a>'
                    self.headers.append(cell)
            self.rows = []

            # for each row in dataset
            for row in dataset:
                onerow = []

                # for each cell in row
                for col_config in config:
                    if col_config.has_key('texttype'):
                        # texttype is mandatory
                        cell=''

                        # link if needed
                        if col_config.has_key('linktype'):
                            cell = cell + '<a href="'
                            if col_config['linktype'] == 's':
                                cell = cell + col_config['linktext'] +
'"'
                            if col_config['linktype'] == 'd':
                                cell = cell + getattr(row,
col_config['linkname'])  + '"'
                            if col_config['linktype'] == 'p':
                                cell = cell + col_config['linktext']
                                cell = cell + getattr(row,
col_config['parmname'])  + '"'
                            if col_config.has_key('linktip'):
                                cell = cell + ' title="' +
col_config['coltip'] + '"'
                            cell = cell + '>'

                        # text
                        if col_config['texttype'] == 'd':
                            cell = cell + getattr(row,
col_config['textname'])
                        if col_config['texttype'] == 's':
                            cell = cell + col_config['text']

                        # close of link
                        if col_config.has_key('linktype'):
                            cell = cell + '</a>'
                        onerow.append(cell)
                self.rows.append(onerow)


TEST CODE - goes in controller.py

from turbogears.widgets.grid import DataGrid2

    @turbogears.expose(html="test2.templates.datagrid")
    def demogrid(self):
        dataset = Author.select()
        config=( {'texttype':'d', 'textname' : 'name', 'colhdr' :
'Name',
                    'coltip':'name of author'},
                  {'texttype':'d', 'textname': 'description','colhdr' :
'Description',
                        'linktype':'s', 'linktext':
'http://www.turbogears.org'},
                  {'texttype':'s', 'text': 'A link', 'colhdr' : 'a
link', 'linktype':'p',
                        'linktext':
'http://www.tubogears.org/wiki=','parmname': 'name'},
                  {'texttype':'s','text': 'static text', 'colhdr' :
'Some Text'}
               )
        griddata=DataGrid2('test', config, dataset)
        return dict( grid=griddata,)

You will notice that there are problems.  The links are not appearing
as links.  They are showing up as text.  How do I fix that?  There can
be a lot more options.  Is this a good way to do it?  I know that I am
not using the fancier python branching that I should.  It can probably
be a ton more efficient.  

Thanks
Alvin

Reply via email to