0
我想創建一些按鈕,其數量將隨用戶操作(列表或數組中的每個值一個)而變化)。使用動態數量的按鈕與Elm
我能夠創建按鈕,但不知道在update
哪一個被按下。
例如,在下面的縮小代碼中,如何增加對應於按下按鈕的data
中的值?
module Main exposing (..)
import Html exposing (..)
import Html.Events exposing (..)
import Array exposing (..)
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ data : Array Int
}
init : (Model, Cmd Msg)
init =
(Model (fromList [ 11, 22, 33 ]), Cmd.none)
-- UPDATE
type Msg
= Play -- or Play Int ?
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Play ->
(model, Cmd.none)
-- VIEW
view : Model -> Html Msg
view model =
div [] (Array.map viewButton model.data |> toList)
viewButton : Int -> Html Msg
viewButton v =
div [] [ button [ onClick Play ] [ text (toString v) ] ]
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none