I just want to share the code that Paolo Caruccio created to show the export dropdown for grid instead of links. It looks very clean and this should be the default for grid layout!
web2py forum link
https://groups.google.com/forum/?fromgroups=#!topic/web2py/HsFWsQmGONM
To test, create new app and add/edit as follows.
Model
db.define_table('Category',
Field('Code', 'integer'),
Field('Name'),
format='%(Name)s')
Controller
def index():
query = db.Category.id >0
grid = SQLFORM.grid(query,csv=True,paginate=10)
return dict(grid=grid)
View
{{extend 'layout.html'}}
{{
if not request.args:
w2p_grid_tbl = grid.element('table')
if w2p_grid_tbl:
original_export_menu = grid.element('div.w2p_export_menu')
export_menu_links = original_export_menu.elements('a')
export_menu_items = []
for link in export_menu_links:
item = LI(link)
export_menu_items.append(item)
pass
new_export_menu = DIV(
A( T('Exports'),
SPAN(_class='caret'),
_href='#',
_class='btn dropdown-toggle',
**{'_data-toggle':"dropdown"}
),
UL(*export_menu_items,
_class='dropdown-menu'
),
_class='w2p_export_menu btn-group'
)
export_menu = grid.element('div.w2p_export_menu',replace=new_export_menu)
pass
pass
}}
{{=grid}}
Before
After
This works for which version of web2py ?? I tried but did not work ... in addition to does not display the drop-down menu as shown .. when I click view or edit an error occurs ... web2py (2.2.1)
ReplyDeleteIt works with version 2.1.1. The first code on the forum doesn't work so please try the code on this blog.
ReplyDeleteDear friend,
DeleteThis works, but when I click view....this line:
export_menu_links = original_export_menu.elements('a')
gives this error : 'NoneType' object has no attribute 'elements'
This comment has been removed by the author.
ReplyDeleteOh, You're correct. I got a same ticket when I click the view. I will ask him on the same forum thread...
ReplyDeletePaolo provided the fixed verstion and I updated the code on this blog. It worked for me now so it should be fine!
ReplyDeleteThanks...it worked....
DeleteDo you know why only view button appears ?
DeleteI was asking the same question to forum and here's answer I got from Simone Niphlod.
ReplyDeleteyou have to be logged-in . By default web2py doesn't allow random users to modify your "precious" data.
you can turn off this safe behaviour with user_signature=False
"""
By default all the URL generated by the grid are digitally signed and verified. This means one cannot perform certain actions (create, update, delete) without being logged-in
"""
http://web2py.com/books/default/chapter/29/07#SQLFORM.grid-and-SQLFORM.smartgrid
Thanks a lot.....much more beautiful
DeleteAwesome logic OBIEE Online Training
ReplyDeleteI tried to put this code in layout.html so that I do not need to put it in every view.
ReplyDeleteIt works when there is grid on page but throws an error when there is no grid. Seems logical ;) but I dont know how to check in layout.html if grid exist.
Great content thanks for sharing this informative blog which provided me technical information keep posting.
ReplyDeleteAws Training in Pune