gpt4 book ai didi

elm - Elm 中的可排序表

转载 作者:行者123 更新时间:2023-12-04 19:53:59 25 4
gpt4 key购买 nike

我正在尝试在 Elm 中设计一个功能,该功能解析来自 Json 的数据,然后将其呈现在一个可排序的表中。

当然,我使用解码器将 Json 数据存储在记录列表中;然后在 View 中,我将记录列表转换为 Dicts 列表,因为我想遍历网格中的数据。我还使用 str 列表、列来为网格中的列提供标题,以确保数据在网格中出现的顺序是可自定义的。

resourceDecoder : Decoder Resource
resourceDecoder =
decode Resource
|> required "year" int
|> required "total_amount" string
|> required "seq_type" string
|> required "sent" bool
|> required "parents_id" int
|> required "month" int
|> required "location_id" int
|> required "child" childDecoder
|> required "id" int

childDecoder : Decoder Child
childDecoder =
decode Child
|> required "firstname" string
|> required "lastname" string

responseDecoder : Decoder (List Resource)
responseDecoder =
Json.Decode.list resourceDecoder

recordToDict : Resource -> ResourceDict
recordToDict record =
Dict.fromList
[ ( "Year", toString record.year )
, ( "Total Amount", record.total_amount )
, ( "Sequence Type", record.seq_type )
, ( "Sent", toString record.sent )
, ( "Parents", toString record.parents_id )
, ( "Month", toString record.month )
, ( "Location", toString record.location_id )
, ( "Firstname", record.child.firstname )
, ( "Lastname", record.child.lastname )
, ( "id", toString record.id )
]

columnsData : List String
columnsData =
[ "Firstname"
, "Lastname"
, "Year"
, "Total Amount"
, "Sequence Type"
, "Sent"
, "Parents"
, "Month"
, "Location"
]

这是问题所在:据我所知,不可能按键的值对字典列表进行排序,因此我必须对值的记录整体进行排序(例如,如果我想按 child 的名字排序,请使用:

List.sortBy (\r -> r.child.lastname) grid.resources

但是,列标题是字符串,并不总是与记录键相同(例如,对于字段 r.child.lastname,列标题是“姓氏”。)无论如何,我的理解是记录键必须被显式调用,因此不可能将列名与记录键匹配。

我希望能够单击表格列并按该字段对其进行排序;例如:



我希望我写的很清楚。谢谢你的帮助!

最佳答案

如何将 columnsData 作为 Dict of
键和标题?
然后用key对List of Dict进行排序?

(我定义了 Key 来说明哪个 String 是资源的关键)

type alias Key = String
columnsData : Dict.Dict String Key
columnsData = Dict.fromList
[ ("Firstname", "firstName")
, ("Lastname", "lastName")
, ("Year", "year")
, ("Total Amount", "totalAmount")
, ("Sequence Type", "seqType")
, ("Sent", "sent")
, ("Parents", "parents")
, ("Month", "month")
, ("Location", "location")
]

sort : Key -> List ResourceDict -> List ResourceDict
sort key dicts =
let
getVal dict = Maybe.withDefault "-" <| Dict.get key dict
in List.sortBy getVal dicts

关于elm - Elm 中的可排序表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39765952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com