Manipulate Rows Object and/or SQLTABLE

99 views
Skip to first unread message

villas

unread,
Nov 9, 2010, 12:36:14 PM11/9/10
to web2py-users
I want to customize the result of SQLTABLE so that it can make me a
nice table without lines and lines of code in my view file. To
achieve that, I need for example to:

1) Add columns to hold icons and links and extra stuff.
2) Customize the rows, e.g. links which depend on content, different
icons etc

After giving it some thought, I think I should leave SQLTABLE alone
and concentrate on 'improving' the rows object so that it contains
everything I want before passing it to SQLTABLE.

To do 1) I can simply add a column to the rows object. How can I best
do that?
To do 2) I could iterate the rows object and make changes.

Or.... maybe there is another way.
I appreciate that if I want to style the HTML table, I'll have to
write my code in the form (which I am trying to avoid).

Thanks,
-D

mdipierro

unread,
Nov 9, 2010, 1:33:38 PM11/9/10
to web2py-users
I see two options:

1) use

db.table.field.represent = lambda value: DIV(value)

and give any representation you want to the field value.

2) If this does not work make your own SQLTABLE helper:

def mytable(rows, cols):
return TABLE(*[TR(*[TD(row[c]) for c in cols]) for row in rows)

villas

unread,
Nov 10, 2010, 5:33:28 AM11/10/10
to web2py-users
The function looks interesting, but I didnt understand how to create
the cols var.

def mytable(rows, cols):
return TABLE(*[TR(*[TD(row[c]) for c in cols]) for row in rows])

I would be grateful for an example how it might work.

I did have this other idea to include an extra column, but I imagine
that it's not so elegant as above.

rows = db(db.test.id>0).select()
extras = list()
for row in rows: extras.append('whatever')

for row,extra in zip(rows,extras):
print TR(TD(row.id),TD(extra))

-D

DenesL

unread,
Nov 10, 2010, 9:30:49 AM11/10/10
to web2py-users
If all you need is an extra column have a look at col3 in SQLFORM
http://web2py.com/book/default/chapter/07#SQLFORM

Ivan Matveev

unread,
Nov 14, 2010, 7:27:10 PM11/14/10
to web2py-users
> I want to customize the result of SQLTABLE so that it can make me a
> nice table without lines and lines of code in my view file.  To
> achieve that,  I need for example to:
>
> 1) Add columns to hold icons and links and extra stuff.
> 2) Customize the rows, e.g.  links which depend on content,  different
> icons etc

You can add any column to select result. The result can be passed to
SQLTABLE.
See:
http://groups.google.com/group/web2py/browse_thread/thread/826a37f56c26d689/210036457d278cdc?lnk=gst&q=patch+to+make+Rows.setvirtualfields+work+with+SQLTABLE#210036457d278cdc

Message has been deleted

villas

unread,
Nov 14, 2010, 7:56:00 PM11/14/10
to web2py-users
Nice one Ivan! Will the patch be included in Web2py? I hope Massimo
likes it.

-D

On Nov 15, 12:27 am, Ivan Matveev <imatvee...@gmail.com> wrote:
> > I want to customize the result of SQLTABLE so that it can make me a
> > nice table without lines and lines of code in my view file.  To
> > achieve that,  I need for example to:
>
> > 1) Add columns to hold icons and links and extra stuff.
> > 2) Customize the rows, e.g.  links which depend on content,  different
> > icons etc
>
> You can add any column to select result. The result can be passed to
> SQLTABLE.
> See:http://groups.google.com/group/web2py/browse_thread/thread/826a37f56c...

Ivan Matveev

unread,
Nov 14, 2010, 8:16:30 PM11/14/10
to web...@googlegroups.com
>  Will the patch be included in Web2py?  I hope Massimo
> likes it.

So do I. Otherwise I will have to patch after every web2py update.

mdipierro

unread,
Nov 15, 2010, 12:20:47 AM11/15/10
to web2py-users
I am confused. Where is the patch?

Ivan Matveev

unread,
Nov 15, 2010, 7:04:28 AM11/15/10
to web...@googlegroups.com
The patch is in the text of this message:

patch to make Rows.setvirtualfields work with SQLTABLE

http://groups.google.com/group/web2py/browse_thread/thread/826a37f56c26d689/210036457d278cdc?lnk=gst&q=patch+to+make+Rows.setvirtualfields+work+with+SQLTABLE#210036457d278cdc

or e-mailing patched sqlhtml.py to you is the right way?

Martin.Mulone

unread,
Nov 15, 2010, 8:23:07 AM11/15/10
to web2py-users

villas

unread,
Nov 15, 2010, 10:11:49 AM11/15/10
to web2py-users
@Martin - I like that!

I think the ability to add rows and columns in an obvious way is
something that has been missing. SQLTABLE needs this feature for
quick 'views'.

However, for DB rows, I think Mr Freeze's Webgrid might be the right
vehicle for further development mainly because we really need the
pagination and set-up options.

I like the idea of using jqGrid, but it just seems a bit too
complex. We need things which 'just work' as part of the framework.

Thanks,
-D


On Nov 15, 1:23 pm, "Martin.Mulone" <mulone.mar...@gmail.com> wrote:
> If it's for small things you can use:
>
> http://groups.google.com/group/web2py/browse_thread/thread/1a1d52d296...

Ivan Matveev

unread,
Nov 15, 2010, 12:24:03 PM11/15/10
to web...@googlegroups.com
Sorry for posting what I'v already posted, but it looks like my post
on the patch to make SQLTABLE work with Rows object with added virtual
fields was lost.

I think the easiest solution to add a column to select result and view
the result in SQLTABLE wold be something like:

class ExtraFields:
def new_column(self):
if self.some_field_in_select==' something':
return A('some_action_link', _href=URL(
f=some_action_controller_function,
args=[self.id]))
else:
return A('great thing', _href='http://www.web2py.com')

rows=db(db.some_table).select()
rows.setvirtualfields(some_table=ExtraFields())
rows.colnames.append('some_table.new_column')
table=SQLTABLE(rows)

This will give an exception telling that

sqlhtml.py:SQLTABLE.__init__

can't do

field = sqlrows.db[tablename][fieldname]

because there is no 'new_column' in the database.
It wants to get the field from the db model to know how to render it.
This doesn't matter when we add a new field to select result
because the new field shell(and will by a view) be cast to string.

----The patch:
change
sqlhtml.py:SQLTABLE.__init__
field = sqlrows.db[tablename][fieldname]
to
try:
field = sqlrows.db[tablename][fieldname]
except:
field = None

change
sqlhtml.py:SQLTABLE.__init__
if field.represent:
r = field.represent(r)
to
if not field:
pass
elif field.represent:
r = field.represent(r)

---Now we are able to include a new field in SQLTABLE
containing anything(text, link, image, button, form, whatever)
depending on row contents(in ExtraFields.new_column(self): 'self' is a single
Row in Rows object returned by select) ,
by adding the field to db().select() result.

---Limitation:
in

rows.colnames.append('some_table.new_column')
rows.setvirtualfields(some_table=ExtraField())

'some_table' shell be a table mentioned in select and present in
the db,
otherwise
'some_table' will be added as a sub dict to Row objects
and you will not see it in SQLTABLE.

Reply all
Reply to author
Forward
0 new messages