web2py:以 CRUD 形式修改小部件
我有一个带有选择小部件的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,这里有一个更简单的方法来替换小部件:
但是,因为您是用新的小部件对象完全替换原始小部件,而不是简单地修改原始小部件,所以
form.custom.widget.customized_field
将仍然参考原来的小部件。因此,您还必须显式替换form.custom.widget.customized_field
。尝试:其中
name
是字段的名称。有关搜索服务器端 DOM 的更多信息,请参阅此处。
另请注意,您可以在创建表单之前(在表定义内或之后)为字段指定自定义小部件:
或
然后,当您创建表单时,将使用自定义小部件而不是默认小部件。
有关小部件的更多信息,请参阅此处。
First, here's a much easier way to replace the 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 replaceform.custom.widget.customized_field
as well. Try: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:
or
Then when you create a form, the custom widget will be used rather than the default widget.
See here for more about widgets.