Half answering my own question.
I found a client-side hack with jQuery. Essentially it simply involves
keeping the form submit event from bubbling up to web2py.js.
def export_rows(selected_ids):
def export():
ret = []
rows = db(db.mytable.id.belongs(selected_ids).select()
for row in rows:
ret.append('%s\r\n' % myformatter(row))
return ret
fileext = 'tab'
content_type = "text/tab-separated-values"
filename = '.'.join(('test', file_ext))
response.headers['Content-Type'] = content_type
response.headers['Content-Disposition'] = \
'attachment;filename=' + filename + ';'
raise HTTP(200, export(), **response.headers)
grid = SQLFORM.grid(db.mytable, selectable=export_rows)
# new from here
grid.append(SCRIPT("""
$('document').ready(function(){
$('div.web2py_grid').on('submit', 'form', function (e) {
e.stopPropagation();
e.stopImmediatePropagation();
return ;
});
});
"""))
return dict(grid=grid)
It isn't perfect, but it's better than nothing. The first time I click on
submit I get the Save As dialog and the submit button stays enabled (OK).
However, when I click it a second time instead of getting another Save As
dialog the page simply reloads, I guess because it's completing POST (?).
I've also tried another approach; adding a js hook into headers for
gluon/main.py to pick it up and pass it to the browser, much like setting
response.js does. Unfortunately it doesn't work. Here it is in all its
kludgyness...
file_ext = 'FAT'
content_type = "text/tab-separated-values"
filename = '.'.join(('SPRING', file_ext))
response.headers['Content-Type'] = content_type
response.headers['Content-Disposition'] = \
'attachment;filename=' + filename + ';'
response.js = 'alert("here we go!");'
import urllib2
response.headers['web2py-component-command'] = \
urllib2.quote(response.js.replace('\n',''))
raise HTTP(200, export(), **response.headers)
On Monday, September 30, 2013 11:33:57 AM UTC+2, step wrote:
>
> I want to use SQLFORM.grid with selectable checkboxes to serve a custom
> export function. Unlike web2py's default exportformats manager, which
> triggers each exporter function to an <a> tag, I want my exporter to
> trigger when the user clicks the form submit button. So the user flow
> involves ticking some grid row checkboxes, clicking submit, getting a Save
> As dialog, and finally saving the downloaded export data as a local file.
>
> controller:
> def export_rows(selected_ids):
> def export():
> ret = []
> rows = db(db.mytable.id.belongs(selected_ids).select()
> for row in rows:
> ret.append('%s\r\n' % myformatter(row))
> return ret
>
>
> fileext = 'tab'
> content_type = "text/tab-separated-values"
> filename = '.'.join(('test', file_ext))
> response.headers['Content-Type'] = content_type
> response.headers['Content-Disposition'] = \
> 'attachment;filename=' + filename + ';'
> raise HTTP(200, export(), **response.headers)
>
>
> return dict(grid = SQLFORM.grid(db.mytable, selectable=export_rows))
>
>
> The above code works to export the rows according to the user flow.
> However, web2py.js disables the submit button when the user clicks it and
> the button is never re-enabled, because raise HTTP transfers control away
> from the page and back to web2py, so the page is never refreshed.
> How to re-enable the submit button, or not disable it to begin with?
> TIA
>
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.