【问题标题】:Web2py: Access row from custom widgetWeb2py:从自定义小部件访问行
【发布时间】:2014-11-24 11:56:46
【问题描述】:

在 web2py 中,自定义小部件获取字段描述和值作为参数,而表示函数获取值和表格行。 是否可以将行传递给自定义小部件功能?我需要访问同一行的其他列。 我在 SQLForm.smartgrid 中使用行,所以在这种情况下我没有太多控制权。

【问题讨论】:

    标签: web2py custom-widgets


    【解决方案1】:

    假设这是用于处理SQLFORM.smartgrid 更新表单,您可以尝试以下技巧:

    def show_grid():
        if 'edit' in request.args:
            db.mytable.myfield.record = db.mytable(request.args(-1))
        return dict(grid=SQLFORM.smartgrid(db.mytable))
    

    上面的代码为字段对象添加了一个“记录”属性(它将被传递给小部件,然后您可以从该字段对象中提取记录)。网格/智能网格“编辑”链接包含记录 ID 作为最后一个 URL arg,可通过上面的 request.args(-1) 访问。

    在您的自定义小部件代码中:

    def mywidget(field, value):
        record = field.record  # here you have the whole record
        ...
    

    【讨论】:

      【解决方案2】:

      小部件方法本身只接收字段和值参数,但是,当您定义小部件对象时,您可以添加更多参数。考虑

      在小部件代码中

      Class CustomWidget():
          def __init__(self, custom_arg_1, custom_arg_2): # Name them as needed
              self.custom_arg_1 = custom_arg_1
              self.custom_arg_2 = custom_arg_2
      
          def widget(field, value):
              if self.custom_arg_1 == self.custom_arg_2:
                  return "Something helpful"
              else:
                  return "Something else"
      

      然后在你的控制器中

      from somewhere import CustomWidget
      
      def this_uses_custom_widget():
          widget_with_args = CustomWidget(3,4) # Pass whatever you need there
          db.table.field.widget = widget_with_args.widget
      

      或者,如果这些参数更全局,您可以在模型中声明小部件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-29
        • 2010-11-16
        • 2022-06-16
        • 1970-01-01
        • 2012-02-17
        • 2014-06-24
        • 1970-01-01
        相关资源
        最近更新 更多