我想創建一個按鈕,當它按下時,將新的輸入(或textarea)添加到窗體。如何在Elm中動態創建輸入?
8
A
回答
9
如果您希望每個按鈕被點擊時要添加的文本字段,這意味着你要顯示的文本字段的數量等於次按鈕被點擊數。我們可以通過按鈕的clicked
信號¹上的countIf id
創建一個信號,告訴我們按鈕被點擊的頻率。
如果我們的輸入列表,我們可以使用flow
到下面(或除)顯示它們彼此。編寫一個函數需要編號n
並生成一個包含按鈕和文本字段的列表。
所以現在我們可以只使用lift
掛鉤這個函數達到我們的信號,計數按鈕的點擊數,結合起來,與該flow
功能,瞧,我們有一個按鈕,動態創建的投入。
(btn, clicked) = Input.button "Click me!"
-- Count how often the clicked signal is true
clicks = countIf id clicked
main = lift2 flow (constant down) $ lift nInputs clicks
nInputs n =
let helper n acc =
if n<=0 then btn : acc
else
-- Input.textField returns a pair containing the field as well as a signal
-- that you can use to access the field's contents. Since I don't actually
-- ever do anything with the contents, I just ignore the signal here.
-- In your real code, you'd probably want to keep the signal around as well.
let (field, _) = Input.textField $ "input " ++ (show n)
in helper (n-1) $ field : acc
in helper n []
¹只要使用count
就可以計算信號改變的頻率。由於每次點擊都會導致信號值變爲true,然後又回到false,那麼每次點擊就會計數2次更改。通過使用countIf id
,我們只計算信號爲真的次數,從而計算點擊次數。
2
從榆木0.8開始,確實可以這樣做。見the release announcement的online reference和code example的輸入組部分。
相關問題
- 1. 在jsf2中創建動態輸入
- 2. 動態創建輸入
- 3. JavaScript-如何在輸入字段中動態創建多個div
- 4. 如何在表單中創建動態輸入?
- 5. Elm中的動態/懶惰導入
- 6. 如何處理React中動態創建的受控輸入?
- 7. Elm中的多個輸入?
- 8. 如何申請自動完成動態創建的輸入框
- 9. 如何使用Elm和elm-mdl爲ID分配輸入?
- 10. 動態創建輸入的問題
- 11. 爲輸入創建動態ng模型
- 12. 動態創建用戶輸入
- 13. 動態創建輸入元素onclick
- 14. 動態創建文件輸入元素
- 15. 創建動態數據輸入表格
- 16. 刪除動態創建的輸入
- 17. 動態創建文本輸入(ASP.net/C#)
- 18. 動態創建用戶輸入表
- 19. 如何在f.select輸入中創建節?
- 20. 用戶輸入如何動態創建對象?
- 21. 如何使用ng-repeat和$ index創建動態輸入
- 22. 我將如何動態創建輸入框?
- 23. 如何設置動態創建的輸入字段的ID?
- 24. 如何從動態創建的表單獲取用戶輸入
- 25. 如何動態創建具有不同ID的輸入元素?
- 26. 如何獲得價值從動態流星創建輸入?
- 27. Js/jQuery - 如何隱藏/顯示動態創建的輸入?
- 28. 如何從動態創建的EditTexts獲取輸入?
- 29. 如何動態刪除使用JavaScript動態創建的輸入字段
- 30. 獲取動態創建的輸入元素的輸入值
請注意,Elm不支持信號信號。如果要存儲文本字段的輸入信號,可以使用Automaton庫:http://elm-lang.org/docs/Automaton.elm – thSoft