【问题标题】:Web2py grid column order rehashed each timeWeb2py 网格列顺序每次都重新散列
【发布时间】:2015-12-13 22:22:34
【问题描述】:

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

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

名字 |年龄 |姓氏 | home_state

重新排列为:

home_state |名字 |姓氏 |年龄

在 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)

###############数据库tbl定义#################

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}}

谢谢,

史蒂文

【问题讨论】:

  • 请显示一些代码,包括数据库表定义、创建网格的控制器以及任何其他相关代码。

标签: gridview web2py


【解决方案1】:

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

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

因此不保持列的顺序。

使用列表而不是集合。

fields=[db.event_record.Email_Description, db.event_record.Modified_at, db.event_record.Email, db.event_record.Title, db.event_record.Event_id]

【讨论】:

  • 将集合更改为列表后,它就像一个魅力!需要更多地关注python方面! @Gaurav Vichare,感谢您的提示!顺便说一句,在网格行中,它将数据库中指向 BLOB 的每个“文件”链接列为普通、丑陋的超链接:>file,在web2py 有没有办法自定义这个?我喜欢用一个图标代替文本“文件”,这样在“文件”上会有更好的外观和感觉。在 SQLFORM.gridm 中似乎没有办法这样做。
  • @StevenZhou 所以你需要下载图标而不是文本“文件”。正确的?您可以在“链接”的帮助下做到这一点。在此处阅读SQLFORM.grid 签名web2py.com/books/default/chapter/29/07/…。如果你没有得到它,我会帮助你!
  • 是的。让我检查一下那个选项。谢谢@Gaurav Vichare!
猜你喜欢
  • 2014-11-18
  • 2019-05-04
  • 2013-04-22
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多