Work with elm and select

后端 未结 5 1097
既然无缘
既然无缘 2021-02-07 04:41

I try to understand how elm works with a custom example.

durationOption duration =
  option [value (toString duration) ] [ text (toString duration)]

view : Mode         


        
5条回答
  •  半阙折子戏
    2021-02-07 05:01

    For future reference for Elm-newbies (like me): with Elm 0.18.0 + elm-lang/html 2.0.0, the onInput event (see code below) works. (Also notice the difference in int range notation (List.range 0 12 instead of [0..12]).

    import Html exposing (..)
    import Html.Attributes exposing (..)
    import Html.Events exposing (onInput)
    
    
    main =
      Html.beginnerProgram
        { model = model
        , view = view
        , update = update
        }
    
    
    
    -- MODEL
    
    
    type alias Model =
      { duration : Int
      }
    
    
    model : Model
    model =
      Model 0
    
    
    
    -- UPDATE
    
    
    type Msg
        = SetDuration String
    
    
    update : Msg -> Model -> Model
    update msg model =
      case msg of
        SetDuration s ->
          let result =
            String.toInt s
          in
            case result of
              Ok v ->
                { model | duration = v }
    
              Err message ->
                model
    
    
    -- VIEW
    
    
    view : Model -> Html Msg
    view model =
      div []
        [ select [ onInput SetDuration ]
                 (List.range 0 12 |> List.map intToOption)
        , div [] [ text <| "Selected: " ++ (toString model.duration) ]         
        ]
    
    
    intToOption : Int -> Html Msg
    intToOption v =
      option [ value (toString v) ] [ text (toString v) ]
    

提交回复
热议问题