SQLFORM.smartgrid() custom field representation breaks exporting functions

40 views
Skip to first unread message

Francisco Ribeiro

unread,
Oct 8, 2017, 8:58:47 PM10/8/17
to web2py-users
hi,

I have customized one of the fields of my SQLForm whereby instead of displaying its long content, it shows a small button that copies its content into the clipboard, as follows:

    db.mytable.myfield.represent = lambda value, row: TAG.button(SPAN(DIV(row.myfield, _id="cp_"+str(row.id),_style="display:none"),_class='glyphicon glyphicon-copy'),_type="button",_onClick="copyToClipboard('#cp_%s'); $.web2py.flash('myfield copied: %s...');"% (row.id,value[:25]), _class="btn-primary")

( copyToClipboard() is a javascript function that pushes contents to the clipboard as the name suggests )

This works fine for most purposes until I try to export the table contents to CSV, HTML and TSV using the SQLForm.smartgrid() generated buttons with an error as follows:
 File ".../gluon/sqlhtml.py", line 3648, in export
self.rows.export_to_csv_file(s, represent=True)
File "/local/path/site-packages/pydal/objects.py", line 2603, in export_to_csv_file
value = field.represent(value, record)
File ".../controllers/analysis.py", line 77, in <lambda>
db.mytable.myfield.represent = lambda value, row: TAG.button(SPAN(DIV(row.myfield, _id="cp_"+str(row.id),_style="display:none"),_class='glyphicon glyphicon-copy'),_type="button",_onClick="copyToClipboard('#cp_%s'); $.web2py.flash('myfield copied: %s...');"% (row.id,value[:25]), _class="btn-primary")
File "/local/path/site-packages/pydal/objects.py", line 99, in __getattr__
raise AttributeError
AttributeError

Exporting to JSON and XML works fine though. Am I doing something wrong? Is there a way to have my form customization working without breaking the export functionality?

Thank you

黄祥

unread,
Oct 8, 2017, 10:22:11 PM10/8/17
to web2py-users
had you already tried to put the copy function in sqlform.grid(links) ?

best regards,
stifan

Francisco Ribeiro

unread,
Oct 9, 2017, 6:28:52 AM10/9/17
to web2py-users
That works, (thanks for the idea btw) but leaves me with a different problem which is that 'myfield' becomes visible and it happens to hold a long string (reason why I came up with the idea to turn it into copy-to-clipboard button). 

Francisco Ribeiro

unread,
Oct 9, 2017, 6:44:06 AM10/9/17
to web2py-users
Well, actually I was able to hide it as well with CSS:
table th:nth-child(7) {display: none;}
table td
:nth-child(7) {display: none;}

Not ideal since all this content is there twice but it works :)
Reply all
Reply to author
Forward
0 new messages