Tuesday, October 23, 2012

web2py grid with export dropdown

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




13 comments:

  1. 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)

    ReplyDelete
  2. It works with version 2.1.1. The first code on the forum doesn't work so please try the code on this blog.

    ReplyDelete
    Replies
    1. Dear friend,

      This 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'

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Oh, You're correct. I got a same ticket when I click the view. I will ask him on the same forum thread...

    ReplyDelete
  5. Paolo provided the fixed verstion and I updated the code on this blog. It worked for me now so it should be fine!

    ReplyDelete
  6. I was asking the same question to forum and here's answer I got from Simone Niphlod.


    you 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

    ReplyDelete
  7. I tried to put this code in layout.html so that I do not need to put it in every view.
    It 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.

    ReplyDelete
  8. Great content thanks for sharing this informative blog which provided me technical information keep posting.
    Aws Training in Pune

    ReplyDelete