質問

質問:

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 を使用しています。

役に立ちましたか?

解決

値パラメータを介して設定します。

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>'
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top