Good work. You also have the option to do

{{=plugin_wiki.widget('jqgrid',table)}}

which is sortable, searchable, resizable, can reorder columns, ajax
pagination, etc.

On Nov 4, 4:38 pm, baloan <balo...@googlemail.com> wrote:
> Final attempt. Much better now. web2py is cool!
>
> def history():
>     headers, key = auto_orderby(db.email_archive,
> default_order=('desc', 'id'))
>     emails = crud.select(db.email_archive, headers=headers,
> orderby=key, limitby=(0, 50))
>     return dict(emails=emails)
>
> def auto_orderby(dal_table, default_order=None):
>     '''
>     Creates headers and key for crud.select() tables.
>
>     @param tablename:      dal table
>     @param default_order:  ('asc'|'desc', column_name)
>     '''
>     tablename = dal_table._tablename
>     orderby_key = tablename + '_orderby'
>     if default_order is None:
>         default_order = ('asc', 'id')
>     # create header column links
>     headers = dict()
>     for col in dal_table.fields:
>         headers[tablename + '.' + col] = A(col,
> _href=URL(vars=dict(orderby=col)))
>     # manage sort order
>     if orderby_key not in session:
>         session[orderby_key] = list(default_order)
>     if 'orderby' in request.vars:
>         if session[orderby_key][1] == request.vars.orderby:
>             if session[orderby_key][0] == 'asc':
>                 session[orderby_key][0] = 'desc'
>             else:
>                 session[orderby_key][0] = 'asc'
>         else:
>             session[orderby_key] = ['asc', request.vars.orderby]
>     # convert to DAL orderby
>     key = db.email_archive[session[orderby_key][1]]
>     if session[orderby_key][0] == 'desc':
>         key = ~key
>     return (headers, key)
>
> Any suggestions for improvement?
>
> Regards, Andreas
>
> On Nov 4, 10:19 pm, baloan <balo...@googlemail.com> wrote:
>
> > An updated attempt:
>
> > def index():
> >     # create header column links
> >     headers = dict()
> >     for col in db.email_archive.fields:
> >         headers['email_archive.' + col] = A(col,
> > _href=URL(vars=dict(orderby=col)))
> >     # manage sort order
> >     if 'email_archive_orderby' not in session:
> >         session.email_archive_orderby = ['desc', 'id']
> >     if 'orderby' in request.vars:
> >         if session.email_archive_orderby[1] == request.vars.orderby:
> >             if session.email_archive_orderby[0] == 'asc':
> >                 session.email_archive_orderby[0] = 'desc'
> >             else:
> >                 session.email_archive_orderby[0] = 'asc'
> >         else:
> >             session.email_archive_orderby = ['asc',
> > request.vars.orderby]
> >     # convert to DAL orderby
> >     key = db.email_archive[session.email_archive_orderby[1]]
> >     if session.email_archive_orderby[0] == 'desc':
> >         key = ~key
> >     emails = crud.select(db.email_archive, headers=headers,
> > orderby=key)
> >     return dict(emails=emails)
>
> > On Nov 4, 10:06 pm, baloan <balo...@googlemail.com> wrote:
>
> > > When I say ugly I mean:
> > > 1. too much duplication,
> > > 2. too much boilerplate code.
>
> > > Just imagine what happens if I apply my solution to more than a few
> > > tables.
>
> > > Regards, Andreas
>
> > > On Nov 4, 10:00 pm, baloan <balo...@googlemail.com> wrote:
>
> > > > This is my feeble attempt at the problem - but it does the job:
>
> > > > def index():
> > > >     add_submenu()
> > > >     headers = {'email.id': A('Id',
> > > > _href=URL(vars=dict(orderby='id'))),
> > > >                'email.email': A('Email address',
> > > > _href=URL(vars=dict(orderby='email'))),
> > > >                'email.active':A('Active',
> > > > _href=URL(vars=dict(orderby='active'))),
> > > >                'email.expires':A('Expires',
> > > > _href=URL(vars=dict(orderby='expires'))),
> > > >                'email.change_user':A('Changed by',
> > > > _href=URL(vars=dict(orderby='change_user'))),
> > > >                'email.change_date':A('Changed on',
> > > > _href=URL(vars=dict(orderby='change_date'))),
> > > >     }
> > > >     if 'email_orderby' not in session:
> > > >         session.email_orderby = ['asc', 'email']
> > > >     if 'orderby' in request.vars:
> > > >         if session.email_orderby[1] == request.vars.orderby:
> > > >             if session.email_orderby[0] == 'asc':
> > > >                 session.email_orderby[0] = 'desc'
> > > >             else:
> > > >                 session.email_orderby[0] = 'asc'
> > > >         else:
> > > >             session.email_orderby = ['asc', request.vars.orderby]
> > > >     key = db.email[session.email_orderby[1]]
> > > >     if session.email_orderby[0] == 'asc':
> > > >         emails = crud.select(db.email, headers=headers, orderby=key)
> > > >     else:
> > > >         emails = crud.select(db.email, headers=headers, orderby= ~key)
> > > >     return dict(emails=emails)
>
> > > > I don't like it - it looks ugly. Who can make it look more elegant?
>
> > > > Best regards, Andreas
> > > > balo...@gmail.com
>
>

Reply via email to