Python Pylons WebHelper optionタグを生成する(h.options_for_select) |
webhelpers.rails.form_options
以前、h.selectを使用してselectタグを生成する方法を
紹介しましたが、selectタグ内のoptionタグはベタに
書かないといけませんでした。
optionタグをリストから生成するには
h.options_for_selectを使用します。
h.selectと組み合わせて
${h.select('select_test', h.options_for_select(["VISA", "MasterCard"]))}
これで
<select id="select_test" name="select_test">
<option value="VISA">VISA</option>
<option value="MasterCard">MasterCard</option>
</select>
こんな感じのHTMLが出力されます。
値と表示する文字を変更したい場合は、リストの中にリストを入れるか、
辞書を渡せばよい模様
${h.select('select_test', h.options_for_select([["Dollar", "$"], ["Kroner", "DKK"]]))}
${h.select('select_test', h.options_for_select(dict(Basic="$20", Plus="$40")))}
初期選択状態にする値を指定したい場合は
${h.select('select_test', h.options_for_select(["VISA", "MasterCard"], "MasterCard"))}
初期表示させたい項目を第二引数に指定すればOKです。
複数選択のリストで複数初期表示の状態を変更したい場合は、
${h.select('select_test', h.options_for_select(["VISA", "MasterCard", "Discover"], ["VISA", "Discover"]),multiple=True)}
こんな感じで、選択させたい項目のリストを渡します。