【发布时间】:2012-03-09 21:22:00
【问题描述】:
我有一个带有选择小部件的 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}}
但是,它仍然向我显示原始未修改的小部件。我究竟做错了什么?有没有更好的方法来做到这一点?
【问题讨论】:
标签: web2py