web2py:以 CRUD 形式修改小部件

发布于 2025-01-07 16:20:36 字数 890 浏览 0 评论 0原文

我有一个带有选择小部件的 CRUD 表单。小部件中的选项是动态的。我无法使用标签,因为这些值来自其他表。我正在尝试使用此方法更改控制器中的值:

def change_widget(form, id, widget):
"Tries to find a widget in the given form with the given id and swaps it with the given widget"
for i in range( len(form[0].components) ):
    if hasattr(form[0].components[i].components[1].components[0], 'attributes'):
        if '_id' in form[0].components[i].components[1].components[0].attributes:
            if form[0].components[i].components[1].components[0].attributes['_id'] == id:
                form[0].components[i].components[1].components[0] = widget
                return True

return False

在调用该方法并检查表单后,我可以看到表单已成功修改。在视图方面,我使用自定义视图并尝试像这样显示表单:

{{=form.custom.begin}} {{=form.custom.widget.customized_field}}
{{=form.custom.submit}} {{=form.custom.end}}

但是,它仍然向我显示原始的未修改的小部件。我做错了什么?有更好的方法吗?

I have a CRUD form that has a select widget. The options in the widget are dynamic. I cannot use tags, as the values are coming from other tables. I am trying to change the values in the controller using this method:

def change_widget(form, id, widget):
"Tries to find a widget in the given form with the given id and swaps it with the given widget"
for i in range( len(form[0].components) ):
    if hasattr(form[0].components[i].components[1].components[0], 'attributes'):
        if '_id' in form[0].components[i].components[1].components[0].attributes:
            if form[0].components[i].components[1].components[0].attributes['_id'] == id:
                form[0].components[i].components[1].components[0] = widget
                return True

return False

After I call the method and inspect the form, I can see that the form has been successfully modified. On the view side, I'm using a custom view and trying to display the form like so:

{{=form.custom.begin}} {{=form.custom.widget.customized_field}}
{{=form.custom.submit}} {{=form.custom.end}}

But, it still shows me the original unmodified widget. What am I doing wrong? Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦亦如幻 2025-01-14 16:20:36

首先,这里有一个更简单的方法来替换小部件:

form.element(_id=id).parent[0] = widget

但是,因为您是用新的小部件对象完全替换原始小部件,而不是简单地修改原始小部件,所以 form.custom.widget.customized_field 将仍然参考原来的小部件。因此,您还必须显式替换 form.custom.widget.customized_field 。尝试:

def change_widget(form, name, widget):
    form.element(_name=name).parent[0] = form.custom.widget[name] = widget

其中 name 是字段的名称。

有关搜索服务器端 DOM 的更多信息,请参阅此处

另请注意,您可以在创建表单之前(在表定义内或之后)为字段指定自定义小部件:

db.define_table('mytable', Field('myfield', widget=custom_widget))

db.mytable.myfield.widget = custom_widget

然后,当您创建表单时,将使用自定义小部件而不是默认小部件。

有关小部件的更多信息,请参阅此处

First, here's a much easier way to replace the widget:

form.element(_id=id).parent[0] = widget

However, because you are completely replacing the original widget with a new widget object rather than simply modifying the original widget, form.custom.widget.customized_field will still refer to the original widget. So, you have to explicitly replace form.custom.widget.customized_field as well. Try:

def change_widget(form, name, widget):
    form.element(_name=name).parent[0] = form.custom.widget[name] = widget

where name is the name of the field.

See here for more about searching the server-side DOM.

Also, note that you can specify a custom widget for the field before the form is even created, either within the table definition, or afterward:

db.define_table('mytable', Field('myfield', widget=custom_widget))

or

db.mytable.myfield.widget = custom_widget

Then when you create a form, the custom widget will be used rather than the default widget.

See here for more about widgets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文