0

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 code
def 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

  • Please show some code, including the database table definition, the controller that creates the grid, and any other related code. – Anthony Sep 17 '15 at 10:49

1 Answers1

0

You defined fields using {} which is set.

A set is an unordered collection with no duplicate elements.

So order of columns is not maintained.

Use list instead of set.

fields=[db.event_record.Email_Description, db.event_record.Modified_at, db.event_record.Email, db.event_record.Title, db.event_record.Event_id]
Gaurav Vichare
  • 1,143
  • 2
  • 11
  • 26
  • After changing the set to a list, it works like a charm! More attention on python side is needed! @Gaurav Vichare, thanks for the tips! btw, in the grid rows, it lists every 'file' link to a BLOB in the database as a plain, ugly hyperlink: >file, in web2py is there a way to customize this? I like to put an icon in place of the text 'file', so it would have better look and feel on a 'file'. seems in SQLFORM.gridm there is no way doing this. – Steven Zhou Oct 09 '15 at 01:54
  • @StevenZhou So you need download icon instead of text 'file'. Right? You can do it with the help of "links". Read `SQLFORM.grid` signature here http://web2py.com/books/default/chapter/29/07/forms-and-validators#SQLFORM-grid . If you didn't get it, I will help you! – Gaurav Vichare Oct 09 '15 at 04:10
  • Yeah. let me check that option. thanks @Gaurav Vichare! – Steven Zhou Oct 09 '15 at 12:57