【问题标题】:How to print index of selected option in Elm?如何在 Elm 中打印所选选项的索引?
【发布时间】:2015-09-06 17:00:22
【问题描述】:

我有一个带有 3 个选项的 <select> HTML 元素和一个 <p> 元素。在<p> 元素中,我想打印<select> 中当前选定项目的索引。例如。如果我选择第一个选项,它应该打印 0,如果我选择第二个选项,它应该打印 1,依此类推。如何从下面给出的最小代码开始?

import Html as H exposing (Html)
import Maybe
import Signal as S exposing (Address, (<~))

type alias Model = { selected : Maybe Int }
model = { selected = Nothing }

type Action = NoOp | Select Int
update action model =
  case action of
    NoOp -> model
    Select n -> { model | selected <- Just n }

view address model =
  H.div []
     [ H.select [] [ H.option [] [ H.text "0" ]
                   , H.option [] [ H.text "1" ]
                   , H.option [] [ H.text "2" ]
                   ]
     , H.p [] [ H.text <| Maybe.withDefault ""
                   <| Maybe.map toString model.selected ]
     ]

actions = Signal.mailbox NoOp
main = view actions.address <~ S.foldp update model actions.signal

【问题讨论】:

    标签: html-select selectedindex elm


    【解决方案1】:

    elm-html 2.0.0 中有很多 different events,但与 &lt;select&gt; HTML 元素无关。所以你肯定需要一个自定义事件处理程序,你可以使用on 创建它。它有一个类型:

    on : String -> Decoder a -> (a -> Message a) -> Attribute
    

    每次在&lt;select&gt; 中选择选项时触发的事件称为“change”。您需要的是来自elm-community/html-extratargetSelectedIndex,它使用了selectedIndex 属性。

    最终代码如下所示:

    更新到 Elm-0.18

    import Html exposing (..)
    import Html.Events exposing (on, onClick)
    import Html.Attributes exposing (..)
    import Json.Decode as Json
    import Html.Events.Extra exposing (targetSelectedIndex)
    
    
    type alias Model =
        { selected : Maybe Int }
    
    
    model : Model
    model =
        { selected = Nothing }
    
    
    type Msg
        = NoOp
        | Select (Maybe Int)
    
    
    update : Msg -> Model -> Model
    update msg model =
        case msg of
            NoOp ->
                model
    
            Select s ->
                { model | selected = s }
    
    
    view : Model -> Html Msg
    view model =
        let
            selectEvent =
                on "change"
                    (Json.map Select targetSelectedIndex)
        in
            div []
                [ select [ size 3, selectEvent ]
                    [ option [] [ text "1" ]
                    , option [] [ text "2" ]
                    , option [] [ text "3" ]
                    ]
            , p []
                [ text <|
                    Maybe.withDefault "" <|
                        Maybe.map toString model.selected
                ]
            ]
    
    
    main : Program Never Model Msg
    main =
        beginnerProgram { model = model, view = view, update = update }
    

    你可以在浏览器中运行它https://runelm.io/c/xum

    【讨论】:

    • 但是,你能不剪吗? :)
    • 包的缩写名称。这个例子对我帮助很大,但我必须在文本编辑器中复制粘贴它并将 JD 更改为 Json.Decode 等......才能理解它。
    • 如果您有时间,很高兴看到此更新为 elm-0.17。如果我觉得我对选择选项的理解足够好,我会尝试这样做。
    • 这是一个适用于 Elm 0.17 的 onSelectonSelect gist
    • onSelect 包含在Exts.Html.Events 中。查看package.elm-lang.org/packages/krisajenkins/elm-exts/25.13.0/…
    猜你喜欢
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 2010-12-08
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多