Orasund/elm-ui-widgets/1.0.0

NOTE: the latest version is 3.4.0

Widget.SortTable

A Sortable list allows you to sort coulmn.

    SortTable.view
        { content =
            [ {id = 1, name = "Antonio", rating = 2.456}
            , {id = 2, name = "Ana", rating = 1.34}
            , {id = 3, name = "Alfred", rating = 4.22}
            , {id = 4, name = "Thomas", rating = 3 }
            ]
        , columns =
            [ SortTable.intColumn
                { title = "Id"
                , value = .id
                , toString = \int -> "#" ++ String.fromInt int
                }
            , SortTable.stringColumn
                { title = "Name"
                , value = .name
                , toString = identity
                }
            , SortTable.floatColumn
                { title = "rating"
                , value = .rating
                , toString = String.fromFloat
                }
            ]
        , model = model
        }
        |> (\{data,columns} ->
            {data = data
            ,columns = columns
                |> List.map (\config->
                        { header =
                            Input.button [Font.bold]
                                { onPress =
                                    { title = config.header
                                    , asc =
                                        if config.header == model.title then
                                            not model.asc
                                        else
                                            True
                                    }
                                        |> SortBy
                                        |> Just
                                , label =
                                    if config.header == model.title then
                                        [ config.header |> Element.text
                                        , Element.text <|
                                            if model.asc then
                                                "/\"
                                            else
                                                "\/"
                                        ]
                                            |> Element.row [Font.bold]
                                    else
                                        config.header  |> Element.text
                                }
                        , view = config.view >> Element.text
                        , width = Element.fill
                        }
                    )
            })
        |> Element.table []

Basics

type alias Model =
{ title : String
, asc : Bool
}

The Model contains the sorting column name and if ascending or descending.

init : Model

The initial State setting the sorting column name to the empty string.

{ content : List a
, columns : List (Column a)
, model : Model
}
-> { data : List a
, columns :
List
{ header : String
, view : a -> String
}
}

The View

{ title : String
, asc : Bool
}
-> Model

Change the sorting criteras.

    sortBy =
        identity

Columns

{ title : String
, value : a -> Int
, toString : Int -> String
}
-> Column a

A Column containing a Int

{ title : String
, value : a -> Float
, toString : Float -> String
}
-> Column a

A Column containing a Float

{ title : String
, value : a -> String
, toString : String -> String
}
-> Column a

A Column containing a String