簡體   English   中英

如何映射列表編號?

[英]How to map numbers of a list?

我有一個包含字符串(播放器)和整數(強度)的列表。 我實現了使用, div [] ( if model.activatedOutput then (List.map (\\{ player} -> div [] [ text player ]) model.teams) else [] )輸出播放器, div [] ( if model.activatedOutput then (List.map (\\{ player} -> div [] [ text player ]) model.teams) else [] )現在我也想輸出存儲在該記錄/列表中的強度值。 但是,獲取參數錯誤。

map的第二個參數不是我期望的:

130 | ,div [](如果為model.activatedOutput,則為(List.map({strength}-> div [] [text strength])model.teams)else []))^^^^^^^^^^^^^ .teams是一個:

 List { activated : Bool, player : String, strength : Int } 

但是map需要第二個參數是:

 List { activated : Bool, player : String, strength : String } 

我認為該錯誤是由於強度必須是要映射的字符串而導致的。 但是我在視圖中將其轉換為字符串。 所以我真的不知道這個錯誤的來源。

這是我代碼的其他一些部分(導致錯誤的行是下面代碼的最后一行):

-- MODEL
type alias Player =
  { player : String
  , strength : Int
  , activated : Bool
  }


type alias Model =
  { content : String
  , teams : List Player
  , currentPlayer : String
  , currentStrength : Int
  , activatedOutput : Bool
  }

-- UPDATE
...
 Add ->
      { model | teams = ({player = model.currentPlayer, strength = model.currentStrength, activated = True} :: model.teams), currentPlayer = "", currentStrength = 0 } 



init : Model
init =
  { content = ""
  , teams = []
  , currentPlayer = ""
  , currentStrength = 0
  , activatedOutput = False

   }
-- VIEW

view : Model -> Html Msg
view model =
  let
    playername = "🏅 Player " ++ String.fromInt (List.length model.teams + 1)
  in
  div []
    [ h1 [style "font-family" "impact"] [ text "Team Creator" ]
    , p [style "font-family" "sans-serif", style "font-size" "15px", style "color" "grey"] [ text "With the Team Creator you can create teams. Insert information about the name and the strength(1-5) of every player and finally how many teams you want to have created by the Team Creator" ]
    ,  h2 [style "font-family" "impact"] [ text "Number of Teams:" ]
    , input [ placeholder  "Number", style "width" "300px", style "height" "30px", style "font-size" "25px", style "color"  "#32db64", value (String.fromInt model.currentNumber), onInput ChangeNumber] []
    ,  h2 [style "font-family" "impact"] [ text "Players per Team:" ]
    , input [ placeholder  "Playernumber", style "width" "300px", style "height" "30px", style "font-size" "25px", style "color"  "#32db64", value (String.fromInt model.currentPlayernumber), onInput ChangePlayernumber] []
    ,  h2 [style "font-family" "impact"] [ text "Name and Strength:" ]
    , div[] [ input [placeholder  playername, style "width" "300px", style "height" "30px", style "font-size" "25px", style "color"  "#488aff", value model.currentPlayer, onInput ChangePlayer] [] ]
    , input [ placeholder  "💪🏼 Strength", style "width" "300px", style "height" "30px", style "font-size" "25px", style "color"  "#4286F5", value (String.fromInt model.currentStrength), onInput ChangeStrength] []
    , div [] [ button [ style "background-color" "#66cc81", style "color" "white", style "margin-top" "20px", style "width" "300px", style "border-radius" "25px", style "height" "40px", style "font-size" "20px", style "margin-right" "70px", onClick Add] [ text "+ADD Player" ] ]
    , div [] [ button [ style "background-color" "#4286F5", style "color" "white", style "margin-top" "10px", style "width" "300px", style "border-radius" "25px", style "height" "40px", style "font-size" "20px", style "margin-right" "70px", onClick Submit] [ text "SUBMIT!" ] ]
    ,  h2 [style "font-family" "impact", style "margin-top" "20px"] [ text "Generated Teams:" ]
    , div [] ( if model.activatedOutput then (List.map (\{ player} -> div [] [ text player ]) model.teams) else [] )
    , div [] ( if model.activatedOutput then (List.map (\{ strength} -> div [] [ text strength ]) model.teams) else [] )
    ]

HTML.text本身不會將數字轉換為String 它的類型為String -> Html msg ,因此已經期望傳遞給它的參數為String 您需要先通過String.fromInt傳遞它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM