Recently I am leveraging Web2py framework plus python in my web programming.
SQLFORM.grid is the best thing happened to Web2py, but after deploying the page with SQLFORM.grid, every time the user refreshes it, the table columns get rehashed, like:
first_name | age | last_name | home_state
is rearranged to:
home_state | first_name | last_name | age
after a F5, and it went on and on. There is no pattern of rearranging between now and next submission. The user experience is effected since they most likely would compare the table between submissions.
This looks wierd and seems not controlled by any switch in .grid() parameters
To be informative as much as possible, one of the field is configured as type string.widget().
09/22/2015: after streching my neck and pulling my hair and battling with my stubborness, the columns are still re-ordering themselves. I am out of ideas and will rather entrust you guys with this. The symptom has been stated previously.
code as below.
############## the codedef email_management():
T.force(None)
web2py_ui = dict(widget='',
header='',
content='',
default='',
cornerall='',
cornertop='',
cornerbottom='',
button='button btn btn-default',
buttontext='',
#buttonadd='icon plus icon-plus glyphicon glyphicon-plus',
buttonadd='icon plus icon-plus',
buttonback='icon leftarrow icon-arrow-left glyphicon glyphicon-arrow-left',
buttonexport='icon downarrow icon-download glyphicon glyphicon-download',
#buttondelete='icon trash icon-trash glyphicon glyphicon-trash',
buttondelete='icon trash',
buttonedit='icon pen icon-pencil glyphicon glyphicon-arrow-pencil',
buttontable='icon rightarrow icon-arrow-right glyphicon glyphicon-arrow-right',
buttonview='icon magnifier icon-zoom-in glyphicon glyphicon-arrow-zoom-in',
)
#jquery style
ui = dict(widget='ui-widget',
header='ui-widget-header',
content='ui-widget-content',
default='ui-state-default',
cornerall='ui-corner-all',
cornertop='ui-corner-top',
cornerbottom='ui-corner-bottom',
button='ui-button-text-icon-primary',
buttontext='ui-button-text',
buttonadd='ui-icon ui-icon-plusthick',
buttonback='ui-icon ui-icon-arrowreturnthick-1-w',
buttonexport='ui-icon ui-icon-transferthick-e-w',
buttondelete='ui-icon ui-icon-trash',
buttonedit='ui-icon ui-icon-gear',
buttontable='ui-icon ui-icon-triangle-1-e',
buttonview='ui-icon ui-icon-zoomin',
)
#process submitted form
if len(request.post_vars) > 0:
for key, value in request.post_vars.iteritems():
(field_name,sep,row_id) = key.partition('_row_') #name looks like home_state_row_99
if row_id:
db(db.event_record.Email == row_id).update(**{field_name:value})
# the name attribute is the method we know which row is involved
db.event_record.Title.represent = lambda value,row: string_widget(db.event_record.Title,value,
**{'_name':'Title_row_%s' % row.id})
if len(request.args) and request.args[0]!= 'None':
db.event_record.Event_id.writable = False
db.event_record.id.readable = False
db.event_record.Email.deletable = False
grid = SQLFORM.grid(db.event_record.Event_id==request.args[0], user_signature=False, searchable=True
, headers={'event_record.id' : 'Email ID', 'event_record.Event_id' : 'Event ID'}
, fields={ db.event_record.Email_Description,db.event_record.Modified_at,db.event_record.Email, db.event_record.Title,db.event_record.Event_id}
, selectable= lambda ids : redirect(URL('default','email_management',vars=request._get_vars))
, exportclasses= dict(xml=False, html=False, json=False, csv=False, tsv=False, tsv_with_hidden_cols=False)
, maxtextlength=80
, showbuttontext=True
, sortable=True
#, ui=ui
, ui=web2py_ui
) #preserving _get_vars means user goes back to same grid page, same sort options etc
grid.elements(_type='checkbox',_name='records',replace=None) #remove selectable's checkboxes
#grid.elements(_class='string',_id='event_record_Email',replace=A('<click>',XML('<b>me</b>'),_href='http://www.web2py.com'))
grid.elements(_type='anchor',replace='test') #remove selectable's submit button
#if grid.accepts(request.vars, session): #.process().accepted:
#if grid.errors:
# response.flash = 'form has errors.'
return dict(grid=grid)
############### DB tbl definition ##################
db.define_table('event_record',
#SQLField('counterparty',requires=IS_NOT_EMPTY()),
#Field('Counterparty_ID', db.counterparty ,requires=IS_NOT_EMPTY()),
#Field('id',requires=IS_NOT_EMPTY(), label = 'Record ID'),
Field('Title',requires=IS_NOT_EMPTY(), label = 'Title', length=200),
Field('Email','upload', autodelete=True),
Field('Event_id', db.counterparty_event, requires=IS_NOT_EMPTY()),
Field('Email_Description', length=40),
Field('Modified_at','datetime',requires=IS_NOT_EMPTY(), default=request.now, writable=True, readable=True),
)
############### the View ###############
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
{{extend 'layout.html'}}
{{=grid}}
thanks,
Steven