how to get the id of the first row in sqlform.grid when it is loaded?

48 views
Skip to first unread message

A3

unread,
Mar 27, 2017, 1:56:10 PM3/27/17
to web2py-users
I am using sqlform.grid, with links that show details in a div when clicked.  

here are some fragments of the code:

def view_report():
    qry.reportnr.represent = render_FIELD(qry.reportnr)
    fields = (qry.id,qry.detail, qry.reportnr,)
    grid = SQLFORM.grid(qr,fields =fields,
                        orderby= qry.reportnr,
                        groupby = qry.reportnr,
                        create = False,
                        deletable = False,
                        editable = False,
                        showbuttontext=False,
                        csv=False,
                        maxtextlength = 64,
                        paginate = 10)
    details = DIV("Details",_id="details")
    return dict(grid=grid, details=details, dummynr="123456")

The View:
view_report.html
     {{=grid}}
     {{=LOAD('default','report_details.load',ajax=True, target='details', user_signature=True ,vars={"Rnr_id":dummynr})}}

def render_FIELD():
    def __call__(self,ids,row,col=None):
... 
        span.append(A(ids,callback=URL('default','report_details.load', vars=dict(id=ids, Rnr_id=row.id)),target='details'))

The loaded  view:
report_details.load
     {{=report.detail}}

The function 
def report_details()
    row_id = request.vars['Rnr_id']
    table = "tbl_reports"
    field = "id"
    q =(db[table][field]==row_id) 
    s = db(q)
    row = s.select().first()
    return dict(report=row)

This works fine. 
When the links are clicked the details are show.

However, when the page loads the first time I need to the dummynr to prevent  an "None" error.

How can I make the page to load and select the top record in the grid automatically and show the details as if the link was clicked?

The same applies to  changing pagination and 

what about handling sorting ? Is it possible to select the top record after the user has clicked the column to be sorted?

Any hint are very welcome.










A3

unread,
Apr 7, 2017, 7:29:12 AM4/7/17
to web2py-users
I made following changes to make it work: 
- When the fields rendered the first row is marked as topfield and stored in session.topfield
- First time load or reload returns row.id = 0 and forces to row.id stored in session.topfield 

 

 def view_report():
    qry.reportnr.represent = render_FIELD(qry.reportnr)
    fields = (qry.id,qry.detail, qry.reportnr,)
    grid = SQLFORM.grid(qr,fields =fields,
                        orderby= qry.reportnr,
                        groupby = qry.reportnr,
                        create = False,
                        deletable = False,
                        editable = False,
                        showbuttontext=False,
                        csv=False,
                        maxtextlength = 64,
                        paginate = 10)
    details = DIV("Details",_id="details")
    return dict(grid=grid, details=details, dummynr="0")

The View:
view_report.html
     {{=grid}}
     <div id="details">
     {{=LOAD('default','report_details.load',ajax=True, target='details', user_signature=True ,vars={"Rnr_id":dummynr})}}

def render_FIELD():
    def __init__(self):
          self.topfield = True
    def __call__(self,ids,row,col=None):
... 
        span.append(A(ids,callback=URL('default','report_details.load', vars=dict(id=ids, Rnr_id=row.id)),target='details'))
        if self.topfield:
               session.topfield = row.id
        self.topfield = False



The loaded  view:
report_details.load
     {{=report.detail}}

The function 
def report_details()
    row_id = request.vars['Rnr_id']
    if row_id == '0': 
           row_id = session.topfield
Reply all
Reply to author
Forward
0 new messages