Extend SQLFORM.grid Action Button functionality

331 views
Skip to first unread message

Christian Espinoza

unread,
Mar 1, 2013, 10:08:05 AM3/1/13
to web...@googlegroups.com
Hi guys, I want to add a action button(like delete,edit, view, i/e) at the grid, something to change a value from the record indicated at the line,
this is my table:

db_events.define_table('alerts',
           
Field('type', length=20, label='Clase' ),
           
Field('rule', label='Regla'),
           
Field('value', length=30, label='Valor'),
           
Field('detail', label='Detalle'),
           
Field('coords', length=20, label='Coordenadas'),
           
Field('checked', 'integer', label='Chequeada', default=0 ),# 1 to checked
           
Field('created_on', 'datetime', label='Recibida', default=request.now)
           
)

The idea is have a button to check the alert, I mean: will be put a 1 at "checked" field of the record when the button will be clicked

Thanks for your time and support!
Christian.

Niphlod

unread,
Mar 1, 2013, 10:31:54 AM3/1/13
to web...@googlegroups.com
disclaimer: not tested: in a controller, prepare a function

def check_to_true():
   
if not request.args(0):
       
return ''
    db_events
.alerts(request.args(0)).update_record(checked=1)

and then pass links in the grid as
links = [
    dict
(header='check the rec',
           body
=lambda row : A('check', _class='btn', _href='#', callback=URL('check_to_true', args=row.id))
         
)]


This should create a clickable button that calls your function and sets the checked to true without leaving the page holding the grid (ajax).
If you need to refresh the values also on the grid page, than a full refresh of the page is needed, so remove the callback argument, use that url in the href and put a redirect(URL('function_showing_the_grid')) at the end of the "check_to_true" function.

Christian Espinoza

unread,
Mar 1, 2013, 10:59:39 AM3/1/13
to web...@googlegroups.com
Wow, Thanks Niphlod!,this code worked like a charm!

Additionally how can I append this code: 

jQuery(this).closest('tr').remove();

to the callback?

Thanks again for your time!
Christian.

Niphlod

unread,
Mar 1, 2013, 11:11:06 AM3/1/13
to web...@googlegroups.com
you didn't ask for that - removing the clicked row - but you're lucky anyway.
add a delete='tr' to the A helper.

http://web2py.com/books/default/chapter/29/05?search=callback#Built-in-helpers

Christian Espinoza

unread,
Mar 1, 2013, 1:59:35 PM3/1/13
to web...@googlegroups.com
Thanks a lot Nihplod, all is working as expected now!!!

Yebach

unread,
Oct 29, 2015, 10:01:21 PM10/29/15
to web2py-users
When I use row.id in links i get an error, row has no attribute id?

My links is in controler before i create SQLFORM.grid.

What am I doing wrong

Vid Ogris

unread,
Oct 29, 2015, 10:31:18 PM10/29/15
to web...@googlegroups.com
Is it possible that is because I have a left join when creating grid?

query=((db.workers_skills.ws_organisation == org) & (db.workers_skills.ws_status == 1))

grid_workersskills= SQLFORM.grid(query=query,left=[db.workers.on(db.workers_skills.ws_worker==db.workers.id),
      db.skills.on(db.workers_skills.ws_skill==db.skills.id)], 
fields=fields,  searchable=False, orderby=[db.workers.w_nick_name],create=True,
deletable=False, editable=True, paginate=50, buttons_placement = 'right',
showbuttontext = False,
links = links, 

--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/asCPsD9UGb8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Lep pozdrav 

Vid Ogris


Reply all
Reply to author
Forward
0 new messages