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

ELM : Onlick button to port the particular string in HTML message to JS when i click the button

我有模型

   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 中,所以我卡住了,哈哈。有什么想法吗?感谢您的帮助!

这里破案了, 在 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 )

无论如何感谢 Jack Leow 的帮助