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 via email to