高度なWebフォームの生成
高度なWebフォームの生成
FormFunctionとFormObjectは簡単なWebフォームを生成するための簡潔なインターフェースを提供するが,ここではより高度なWebフォームを生成するのに必要な方法やオプションについて説明する.
FormFunctionとFormObjectはオプションAppearanceRulesを取る.これはタイトル,および要素が各フィールドでどのように表示されるか等のフォームのさまざまな部分を指定するために使うことができる.
TitleとDescription
CloudDeploy[FormFunction["city" -> "City", #city&, AppearanceRules -> <|"Title" -> "What's your city", "Description" -> "This form allows you to input your city"|>]]SubmitLabelとNextLabel
CloudDeploy[FormFunction["city" -> "City", #city&, AppearanceRules -> <|"SubmitLabel" -> "Go!"|>]]テキスト以外の値
AppearanceRulesのフィールドのほとんどは文字列である必要はない.
CloudDeploy[FormFunction["animal" -> "Animal", #animal&, AppearanceRules -> <|"SubmitLabel" -> [image], "Title" -> Import["ExampleData/turtle.jpg"], "Description" -> Style["What is this animal?", Blue] |>]]RequiredFieldIndicator
CloudDeploy[FormFunction[{"required" -> "Number", "optional" -> "Number" -> 0}, Identity, AppearanceRules -> <|"RequiredFieldIndicator" -> Function[Style[#, Red]] |>]]ItemLayout
"ItemLayout"は,要素をどのように格子に表示するかを決めるために使うことができる.空の行や列は自動的に省略される.項目のレイアウトはレスポンシブである.つまりレイアウトがブラウザウィンドウの幅に応じて変化する.特に,項目のレイアウトは携帯端末のWebブラウザでは異なって見える場合がある.
CloudDeploy[FormFunction[{"x" -> <|"Help" -> "type here"|>, "y" -> "Integer"}, Identity, AppearanceRules -> <|"ItemLayout" -> "Horizontal" |>]["x" -> "text"]]CloudDeploy[FormFunction[{"x" -> <|"Help" -> "type here"|>, "y" -> "Integer"}, Identity, AppearanceRules -> <|"ItemLayout" -> "Inline" |>]["x" -> "text"]]CloudDeploy[FormFunction[{"x" -> <|"Help" -> "type here"|>, "y" -> "Integer"}, Identity, AppearanceRules -> <|"ItemLayout" -> "Vertical" |>]["x" -> "text"]]CloudDeploy[FormFunction[{{"first" -> "Integer"}, {"second" -> "Integer"}}, Identity]]CloudDeploy[FormFunction[{{"label" -> "String"}, {{"second", #label } -> "Integer"}&}, Identity]]CloudDeploy[FormFunction[{{"number" -> "Integer"}, Table["x" <> ToString[i] -> "String", {i, #number}]&}, Identity]]1つのフィールドが他のフィールドに依存するフォームや,両方のフィールドが同一ページに現れるフォームを定義すると便利なことがある.これはWolfram言語のテンプレート言語でTemplateSlot等のシンボルを使って行うことができる.
CloudDeploy[FormFunction[{"state" -> "USState", "county" -> TemplateSlot["state"][EntityProperty["AdministrativeDivision", "Subdivisions"]]}, Identity]]フィールドをAutoSubmittingでラップすると,それが記入されたときにフォーム全体を自動的に送信させることができる.
CloudDeploy[FormFunction[{"x" -> AutoSubmitting["Integer"], "y" -> "Integer"}, Identity]]フォームにさまざまな数のフィールドがあると便利なことがある.RepeatingElementを使うと,フィールドを繰り返すことができる.
CloudDeploy[FormFunction["field" -> RepeatingElement["String", {2, {1, 5}}]]]RepeatingElementの第2引数を使うと,サブフィールドをその指標に依存させることができる.
CloudDeploy[FormFunction["number of occupants" -> RepeatingElement[<|"Interpreter" -> "Integer", "Label" :> "room " <> ToString[i]|>, {{i, 2}, {1, 5}}]]]CloudDeploy[FormFunction["names" -> RepeatingElement[CompoundElement[<|"first" -> "String", "last" -> "String"|>]]]]CloudDeploy[FormFunction["field" -> RepeatingElement["String"], AppearanceRules -> <|"AddLabel" -> "Add", "DeleteLabel" -> "Delete"|>]]