我想链接一个滑块和一个输入文本区域,这样当我更改一个时,另一个会自动更新。我找到了elm-reactor的实现,它使用本机JavaScript并使用回调。移动滑块时会调用回调,但是当从文本区域更改值时,我无法移动它...
答案 0 :(得分:18)
实际上并不难。此代码显示如何设置两种输入类型的值。
import Html exposing (div, text, input)
import Html.Attributes as H exposing (..)
import Html.Events as E
mbox =
Signal.mailbox "0"
view v =
let
evth = E.on "change" E.targetValue (Signal.message mbox.address)
in
div []
[ input
[ type' "range"
, H.min "0"
, H.max "20"
, value v
, evth
] []
, input
[ type' "text", value v, evth ] []
]
main =
Signal.map view mbox.signal
更新0.17
import Html exposing (Attribute, div, text, input)
import Html.App as Html
import Html.Attributes as H exposing (..)
import Html.Events exposing (on, onInput)
import Json.Decode exposing (string, map)
import String
type alias Model = Int
type Msg
= Update String
update : Msg -> Model -> Model
update (Update v) model =
String.toInt v |> Result.withDefault 0
view model =
div []
[ input
[ type' "range"
, H.min "0"
, H.max "20"
, value <| toString model
, onInput Update
] []
, text <| toString model
]
main =
Html.beginnerProgram
{ model = 0
, view = view
, update = update
}
更新0.18
import Html exposing (Attribute, div, text, input)
import Html.Attributes as H exposing (..)
import Html.Events exposing (on, onInput)
import Json.Decode exposing (string, map)
import String
type alias Model = Int
type Msg
= Update String
update : Msg -> Model -> Model
update (Update v) model =
String.toInt v |> Result.withDefault 0
view model =
div []
[ input
[ type_ "range"
, H.min "0"
, H.max "20"
, value <| toString model
, onInput Update
] []
, text <| toString model
]
main =
Html.beginnerProgram
{ model = 0
, view = view
, update = update
}
答案 1 :(得分:3)
基本思想是在模型中对数字进行一次规范表示。每当它发生变化(这将是一个动作)时,你都不关心变化的来源,你只需要更新模型。然后根据模型中数字的值渲染文本区域和滑块。 (我们只是用一个单独的文本字段来说话,而不是一个自由形式的段落框,对吧?)
希望有所帮助!
答案 2 :(得分:1)
来自Simon H的原始答案, Elm 0.18的更新:
import Html exposing (Attribute, div, text, input, beginnerProgram)
import Html.Attributes as H exposing (..)
import Html.Events exposing (on, onInput)
type alias Model = Int
type Msg
= Update String
update : Msg -> Model -> Model
update (Update v) model =
String.toInt (Debug.log "" v) |> Result.withDefault 0
view model =
div []
[ input
[ type_ "range"
, H.min "0"
, H.max "20"
, value <| toString model
, onInput Update
] []
, text <| toString model
]
main =
Html.beginnerProgram
{ model = 0
, view = view
, update = update
}
答案 3 :(得分:1)
从banncee的更新版本更新为Elm 0.19 ,从Simon H的原始答案更新到版本0.18
import Browser
import Html exposing (Attribute, div, text, input)
import Html.Attributes as H exposing (..)
import Html.Events exposing (on, onInput)
type alias Model = Int
type Msg
= Update String
init : Model
init = 0
update : Msg -> Model -> Model
update (Update v) model =
case String.toInt v of
Just i -> i
Nothing -> model
view model =
div []
[ input
[ type_ "range"
, H.min "0"
, H.max "20"
, value <| String.fromInt model
, onInput Update
] []
, text <|String.fromInt model
]
main =
Browser.sandbox
{ init = init
, view = view
, update = update
}
答案 4 :(得分:0)
同时,有一些库可以创建滑块: