ELM:当我单击按钮时,将HTML消息中的特定字符串移植到JS的Onlick按钮

问题描述 投票:0回答:1

我有模特

   type alias Model =
    { 
      url : String
    , devices : List Device
    , input : String
    , isFilter : Bool
    , deviceSuggestion : Maybe Device
    }

type alias Device =
    { 
      status : String
    , license_plate : String
    , device_id : String
    }

和下面是我的渲染视图,我想获得device_id和JS的端口

renderPost : Device -> Html Msg
renderPost devices =
div []
    [
       [ text "Device_id : " 
        , button [ onClick PortDeviceID ] [ text (Debug.toString devices.device_id) ]
        , pre [] [ text "device_model : " , text (Debug.toString devices.device_model) ]  
        , pre [] [ text "license_plate : " , text (Debug.toString devices.license_plate) ]

     ]

renderPosts : Model -> Html Msg
renderPosts model =
([ text "Device List" ] ++ List.map (\device -> renderPost device) model.devices)

我的更新在这里。我不知道如何获取文本,因为devices.device_id来自json文件

    PortDeviceID ->
         ( { model | deviceID = "" }, //port device_id --> not working)
    GotResult result ->
        case result of
            Ok devices ->
                ( { model | devices = devices }, portDevice devices ) // this give full list

我想做的是,当我单击按钮时,我可以将特定的device_id移植到JS。问题是它在Device -> Html Msg format中,所以我卡住了。有什么想法吗?任何帮助表示赞赏!

list port elm
1个回答
0
投票

案例在这里解决了,在View中,我确实像下面这样,实例化devices.device_id

renderPost : Device -> Html Msg
renderPost devices =
div []
    [
       [ text "Device_id : " 
        , button [ onClick (PortDeviceID devices.device_id)] [ text (Debug.toString devices.device_id) ]

然后更新,同样让portDeviceID String

 PortDeviceID deviceID->
         ( { model | deviceID = "" }, portDeviceID deviceID )

无论如何都要感谢杰克·莱奥的帮助

© www.soinside.com 2019 - 2024. All rights reserved.