Thanks for your reply! Is there a better way to do this or this is a common 
solution in elm?

Le lundi 10 octobre 2016 13:54:13 UTC+2, Did a écrit :
>
> Hi there,
>
> I'm pretty new to elm, so maybe this is a common issue.. I have a list of 
> articles, each article is composed of an id, a name, a description and a 
> price. I have also, in the UI, 3 fields in order to create a new article 
> and add it to the list by clicking to the "click" button. All articles are 
> listed in a table.
>
> But I really don't understand how to resolve this... Maybe by having an 
> article object and "bind" its properties with a onInput event on their 
> corresponding field, but even by doing that, I'm not sur how to implement 
> the update function. For now, the update function returns the model because 
> I don't know how to implement each cases...
>
> Thanks for your help!
>
> Here is the code :
>
> import Html exposing (..)
> import Html.App as App
> import Html.Events exposing (onClick, onInput)
> import Html.Attributes exposing (..)
>
> main =
>   App.beginnerProgram {model = model, update = update, view = view}
>  
> type alias Model = List Article
>
> type alias Article = 
>   { 
>     id: Int
>    ,name: String
>    ,description: String
>    ,price: String
>   }
>
> model: Model
> model = [{
>     id = 1
>    ,name = "Test 1"
>    ,description = "Description..."
>    ,price = "10.0"
>   }]
>  
>
> type Msg = 
>    Run | UpdateName String | UpdateDescription String | UpdatePrice String
>
>
> update: Msg -> Model -> Model
> update msg model =
>     case msg of
>     UpdateName newName -> model
>     UpdateDescription newDesc -> model
>     UpdatePrice newPrice -> model
>     Run                 -> model
>
>
> view model =
>   let th' field = th [] [text field]
>       tr' article = tr [] [ td [][text article.name] 
>                            ,td [][text article.description]
>                            ,td [][text article.price]
>                           ]
>   in
>   div []
>   [input[type' "text", placeholder "Enter a name...", onInput UpdateName][]
>    ,input[type' "text", placeholder "Enter a description...", onInput 
> UpdateDescription][]
>    ,input[type' "text", placeholder "Enter a price...", onInput 
> UpdatePrice][]
>    ,div[]
>    [
>      input[type' "button", value "click", onClick Run][]
>    ]
>    ,div[]
>    [
>     table [] [
>       thead[][tr [](List.map th' ["name", "description", "price"])]
>      ,tbody[](List.map tr' model)
>     ]
>    ]
>   ]
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to