2016-05-22 8 views
13

elmがカスタム例でどのように動作するかを理解しようとしています。elmと連携して選択してください

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

view : Model -> Html Msg 
view model = 
    Html.div [] 
    [ h2 [] [ text "Month selector"] 
    , select [] 
     (List.map durationOption [1..12])  
    ] 

これは、selectで操作する簡単な例です。私は、月の値を変更するたびに値を10倍します。ドキュメントによると、onChangeonSelectのようなイベントはありません。onで私を作成しなければなりませんか?

答えて

15

はい、変更イベントを処理するには、onを使用する必要があります。 onClickのように、Elmに組み込まれたsource for other event handlersを見ると、それらはすべてon関数を使って構築されていることがわかります。

targetValueIntParseからelm-community/html-extraまでを使用して、オプションから文字列値をintに変換する例を示します。エルム-0.18

import Html exposing (..) 
import Html.Events exposing (on) 
import Html.Attributes exposing (..) 
import Json.Decode as Json 
import String 
import Html.Events.Extra exposing (targetValueIntParse) 


main = 
    beginnerProgram { model = { duration = 1 }, view = view, update = update } 


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


view : Model -> Html Msg 
view model = 
    Html.div [] 
     [ h2 [] [ text "Month selector" ] 
     , select [ on "change" (Json.map SetDuration targetValueIntParse) ] 
      (List.map durationOption (List.range 1 12)) 
     , div [] [ text <| "Selected: " ++ (toString model.duration) ] 
     ] 


type Msg 
    = SetDuration Int 


type alias Model = 
    { duration : Int } 


update msg model = 
    case msg of 
     SetDuration val -> 
      { model | duration = val } 

に更新

あなたは(私のような)エルム - 初心者のための今後の参考のために、ブラウザhttps://runelm.io/c/ahz

+0

ご回答いただきありがとうございます。私は、コードのこの部分に追加いただきましundertandするより多くのスキルを必要とします。 – billyJoe

+3

'onChange'が標準ライブラリに存在しないのはなぜですか? – DenisKolodin

+0

' option'要素で 'Html.Attributes.selected = True'を使うと、選択したオプションが常に正しく表示されていることを確認できます。 –

21

でこの例を実行することができます:エルム0.18.0 + ELM-LANGと/ html 2.0.0、onInputイベント(下記のコードを参照)works (また、int型の範囲表記(List.rage 0 12の代わりに[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) ] 
+3

これは答えとしてマークしてはいけない理由はありますか?あまり手間がかかりません –

+2

現時点ではonChangeとブラウザ間で互換性がありません。残念ながらhttp://caniuse.com/#search= oninput –

+0

Opera Miniを除くすべてのものは互換性があります。 –

関連する問題