Elm 从列表中查找一项
榆树新来的。。 我正在尝试从列表中获取一项 这是模型(第一次发布时忘记) 我假设Report.id必须是IntElm 从列表中查找一项,elm,Elm,榆树新来的。。 我正在尝试从列表中获取一项 这是模型(第一次发布时忘记) 我假设Report.id必须是Int -- MODEL type alias Model = { reports : List Report , alertMessage : Maybe String } type alias Report = { id : Int , city : String , country : String } initialM
-- MODEL
type alias Model =
{ reports : List Report
, alertMessage : Maybe String
}
type alias Report =
{ id : Int
, city : String
, country : String
}
initialModel : Model
initialModel =
{ reports = []
, alertMessage = Nothing
}
-- UPDATE
type Msg
= NewReports (Result Http.Error (List Report))
| ShowReport
| CloseAlert
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NewReports (Ok weatherReports) ->
( { model | reports = weatherReports }, Cmd.none )
NewReports (Err error) ->
( { model | alertMessage = Just (httpErrorToMessage error) }, Cmd.none )
CloseAlert ->
( { model | alertMessage = Nothing }, Cmd.none )
ShowReport model ->
( ??? )
--VIEW now more complete to get the right picture
viewReportRow : Report -> Html Msg
viewReportRow report =
tr []
[ td [] [ text report.city ]
, td [] [ text report.country ]
, td [] []
[ button
[ class "button primary small"
, onClick (ShowReport report.id)
]
[ text "Show"]
]
]
如何正确地从按钮发送id,并在列表中筛选具有此id的条目
感谢您的支持给您
我更新了ShowReport消息以获取id(我假设是字符串),演示了如何进行筛选(由您决定如何处理生成的列表),并将视图片段转换为可以映射到model.reports的函数
这只是指示性的,因为您没有提供模型的太多细节
type Msg
= NewReports (Result Http.Error (List Report))
| ShowReport String
| CloseAlert
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NewReports (Ok weatherReports) ->
( { model | reports = weatherReports }, Cmd.none )
NewReports (Err error) ->
( { model | alertMessage = Just (httpErrorToMessage error) }, Cmd.none )
CloseAlert ->
( { model | alertMessage = Nothing }, Cmd.none )
ShowReport id ->
let report =
List.filter (\rep -> rep.id == id) model.reports
in
(model, Cmd.none)
--VIEW
mkShowItem id =
td []
[
button
[ class "button small"
, onClick (ShowReport id)
]
[ text "Show"]
]
给你
我更新了ShowReport消息以获取id(我假设是字符串),演示了如何进行筛选(由您决定如何处理生成的列表),并将视图片段转换为可以映射到model.reports的函数
这只是指示性的,因为您没有提供模型的太多细节
type Msg
= NewReports (Result Http.Error (List Report))
| ShowReport String
| CloseAlert
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NewReports (Ok weatherReports) ->
( { model | reports = weatherReports }, Cmd.none )
NewReports (Err error) ->
( { model | alertMessage = Just (httpErrorToMessage error) }, Cmd.none )
CloseAlert ->
( { model | alertMessage = Nothing }, Cmd.none )
ShowReport id ->
let report =
List.filter (\rep -> rep.id == id) model.reports
in
(model, Cmd.none)
--VIEW
mkShowItem id =
td []
[
button
[ class "button small"
, onClick (ShowReport id)
]
[ text "Show"]
]
谢谢你的支持,西蒙。我用丢失的模型完成了代码。。ShowReport仍然抛出错误“model.reports”似乎不够。function
filter
的第二个参数导致不匹配。78 | List.filter(\item->item==id)model.reports^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^。我用丢失的模型完成了代码。。ShowReport仍然抛出错误“model.reports”似乎不够。functionfilter
的第二个参数导致不匹配。78 | List.filter(\item->item==id)model.reports^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^