问题:
如何使用值从ToscaWidgets预填充CheckBoxTable。
背景
我到处寻找,我似乎无法弄清楚如何使用ToscaWidgets初始化特定的表单字段。大多数表单字段似乎都对初始化做出了很好的响应,就像我在模板中渲染表单时创建一个包含单个TextField的表单并传入fieldValue = x其中fieldValue是TextField的名称而x是一些字符串TextField将填充x。我的问题是所有多个选择字段,特别是CheckBoxTable。无论我传入什么内容都不会初始化多重选择。这是我正在谈论的一个例子,它是一个带有CheckBoxTable的用户编辑页面,因此你可以从数据库中提取的几个组的列表中选择几个或没有组:
我有什么:
我的小部件是:
from tw import forms
class UserForm(forms.TableForm):
show_errors = True
submit_text = "Create User"
clientOptions = [(-1, "Select a Client")]
groupOptions = [(-1, "Select a Group")]
fields = [forms.TextField('name', label_text='User Name', validator=String(not_empty=True), size=40),
forms.Spacer(),
forms.SingleSelectField('clientID', label_text='Client Name', validator=Int(min=0), options=clientOptions),
forms.Spacer(),
forms.CheckBoxTable('groups', lable_text='Groups', validator=Set(), options=groupOptions, num_cols=3),
forms.Spacer(),
forms.PasswordField('password', label_text="Password", validator=String(not_empty=True, min=6), size=40),
forms.PasswordField('passwordAgain', label_text="Repeat Password", validator=String(not_empty=True, min=6), size=40),
forms.HiddenField('id')]
editUserForm = UserForm("createUserForm", action='alterUser', submit_text="Edit User")
在我的控制器中我有:
result = model.DBSession.query(model.User).filter_by(id=kw['id']).first()
tmpl_context.form = editUserForm
clientOptions=model.DBSession.query(model.Client.id, model.Client.name)
groupOptions=model.DBSession.query(model.Group.id, model.Group.name)
formChildArgs = dict(clientID=dict(options=clientOptions), groups=dict(options=groupOptions))
userAttributes=dict(id=result.id, name=result.name, groups=[g.id for g in result.groups], clientID=result.clientID, password=result.password, passwordAgain=result.password)
return dict(verb="Edit", modelName = "User", modelAttributes=userAttributes, formChildArgs=formChildArgs, page='editUser')
在我的模板(Mako)中我有:
${tmpl_context.form(modelAttributes, child_args=formChildArgs) | n}
我尝试了什么:
在我的userAttributs字典中,我尝试过:
groups=[g.id for g in result.groups]
groups=[g.name for g in result.groups]
groups=[(g.id, g.name) for g in result.groups]
groups=[[g.id, g.name) for g in result.groups]
groups=result.groups
我得到了什么:
所有这些代码的结果都是一个用户编辑表单,其中的数据预填充了除CheckBoxTable之外的用户数据。 CheckBoxTable让我的数据库中的所有组都显示并清空,我需要它们才能显示它们,但让用户分开检查的组。我认为模型属性中的代码会这样做,因为这就是它对其他每个字段的作用,但是我必须有一些关于CheckBoxTable实例化的基本内容。
功能
我正在使用Turbogears 2和ToscaWidgets 0.9.7表单以及Mako进行模板化。
答案 0 :(得分:1)
通过值参数设置它们。
import tw.forms
f = tw.forms.TableForm(fields=[tw.forms.CheckBoxTable("name",options=(("foo"),("bar")))])
f(value={"name":{"foo":True,"bar":False}})
>>> u'<form xmlns="http://www.w3.org/1999/xhtml" action="" method="post" class="tableform">\n <table border="0" cellspacing="0" cellpadding="2">\n<tr id="name.container" class="even" title="">\n <td class="labelcol">\n <label id="name.label" for="name" class="fieldlabel">Name</label>\n </td>\n <td class="fieldcol">\n <table id="name" class="checkboxtable">\n <tbody>\n <tr>\n <td>\n
<input id="name_0" value="foo" name="name" type="checkbox" checked="checked" />\n <label for="name_0">foo</label>\n </td>\n </tr><tr>\n <td>\n <input id="name_1" value="bar" name="name" type="checkbox" />\n <label for="name_1">bar</label>\n </td>\n </tr>\n
</tbody>\n</table>\n </td>\n </tr><tr id="submit.container" class="odd" title="">\n <td class="labelcol">\n </td>\n
<td class="fieldcol">\n <input type="submit" class="submitbutton" value="Submit" />\n </td>\n </tr>\n </table>\n</form>'