每次都重新标记Web2py网格列顺序

时间:2015-09-17 06:19:56

标签: gridview web2py

最近我在网络编程中利用了Web2py框架和python。

SQLFORM.grid是Web2py发生的最好的事情,但是在用SQLFORM.grid部署页面后,每次用户刷新它时,表列都会被重新散列,如:

first_name |年龄| last_name | home_state

重新排列为:

home_state | first_name | last_name |年龄

在F5之后,它继续进行。现在和下一次提交之间没有重新排列的模式。用户体验受到影响,因为他们很可能会比较提交的表格。

这看起来很奇怪,似乎不受.grid()参数

中任何开关的控制

为尽可能提供信息,其中一个字段配置为string.widget()类型。

2015年9月22日:拉伸我的脖子,拉扯我的头发并与我的顽固对抗后,柱子仍在重新排序。我没有想法,宁愿委托你们。之前已经说过这种症状。

代码如下。

############## 代码
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}}

感谢,

史蒂芬

1 个答案:

答案 0 :(得分:0)

您使用设置的{}定义了字段。

  

集合是无序集合,没有重复元素。

因此不保持列的顺序。

使用list而不是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]