-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableExample.elm
131 lines (96 loc) · 2.73 KB
/
TableExample.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
module TableExample exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import SortableTable as Table
-- ****
-- Model
-- ****
type alias CityData =
{ city : String
, country : String
, population : Int
}
type alias Model =
{ cities : List CityData
, sortState : Table.SortState
}
initialModel : Model
initialModel =
{ cities =
[ CityData "Shanghai" "China" 24153000
, CityData "Beijing" "China" 18590000
, CityData "Karachi" "Pakistan" 18000000
, CityData "Istanbul" "Turkey" 14657000
, CityData "Dhaka" "Bangladesh" 14543000
, CityData "Tokyo" "Japan" 13617000
, CityData "Moscow" "Russia" 13197596
, CityData "Manila" "Philippines" 12877000
, CityData "Tianjin" "China" 12784000
, CityData "Mumbai" "India" 12400000
]
, sortState = Table.initSortState "Population" Table.Desc
}
-- ******
-- Update
-- ******
init : ( Model, Cmd Msg )
init =
( initialModel, Cmd.none )
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
ReSort newSortState ->
-- Typically make request for re-sorted data here
( { model | sortState = newSortState }, Cmd.none )
-- ********
-- Messages
-- ********
type Msg
= ReSort Table.SortState
-- ****
-- View
-- ****
cellPadding : Attribute msg
cellPadding =
style
[ ( "padding-top", "1rem" )
, ( "border-bottom", "1px solid #989898" )
, ( "padding-right", "0.5rem" )
]
tableConfig : Table.Config CityData Msg
tableConfig =
Table.createConfig
[ Table.defineColumn "City" (text << .city) [ cellPadding ]
, Table.defineColumn "Country" (text << .country) [ cellPadding ]
|> Table.makeSortable ReSort
, Table.defineColumn "Population" (text << toString << .population) [ cellPadding ]
|> Table.makeSortable ReSort
]
(Table.defaultCustomizations
|> Table.customizeSort
[ style
[ ( "height", "16px" )
, ( "vertical-align", "sub" )
, ( "fill", "#989898" )
]
]
|> Table.customizeTable
[ style [ ( "border-collapse", "collapse" ) ] ]
)
view : Model -> Html Msg
view { cities, sortState } =
div [ id "main", style [ ( "margin", "4rem" ) ] ]
[ h2 [] [ text "City Populations" ]
, div [] [ Table.view tableConfig sortState cities ]
]
-- ***
-- App
-- ***
main : Program Never Model Msg
main =
Html.program
{ init = init
, update = update
, subscriptions = \_ -> Sub.none
, view = view
}