Ooof. Looking at your soln, I see that I was so focused on re-ordering
those `[:tr ..]`'s that I didn't realize I should first sort the source
data I start with *before* converting it to hiccup data structures.
Thanks!
-- John
On Friday, February 24, 2017 at 6:11:19 PM UTC-5, James Reeves wrote:
>
> Generally you'd approach the problem by using an intermediate structure.
> For example:
>
> (def data
> {:headers [:color :size]
> :rows [{:color "blue", :size 4}
> {:color "red", :size 2}
> {:color "green", :size 7}])
>
> Then you could build a function that translates this data structure into a
> Hiccup table:
>
> (defn ->table [{:keys [headers rows]}]
> [:table
> [:tr (for [h headers] [:th (str/capitalize h)])]
> (for [row rows]
> [:tr (for [h headers, :let [v (row h)] [:td h])])])
>
> Then you could write something like:
>
> (->table (update data :rows (partial sort-by :color))
>
> - James
>
> On 24 February 2017 at 22:41, John Gabriele <[email protected]
> <javascript:>> wrote:
>
>> I'm using hiccup, and I'd like to build a table, but then have the option
>> to re-order it (sort its rows by a column of my choosing). I want a
>> `sort-table-by` function.
>>
>> That is, if I've got
>>
>>
>> Color Size
>> ------ ----
>> red 2
>> green 7
>> blue 4
>>
>>
>> (or, as a data structure: `(def tbl [:table [:tr [:th "Color"] [:th
>> "Size"]] [:tr [:td "red"] [:td 2]] [:tr [:td "green"] [:td 7]] [:tr [:td
>> "blue"] [:td 4]]])`)
>>
>> I'd like to be able to do `(sort-table-by tbl 0)` and get
>>
>>
>> Color Size
>> ------ ----
>> blue 4
>> green 7
>> red 2
>>
>>
>> or `(sort-table-by tbl 1)` and get
>>
>>
>> Color Size
>> ------ ----
>> red 2
>> blue 4
>> green 7
>>
>>
>> Where it gets tricky though is when your table has various attribute maps
>> in it, and also when some of the values you're sorting on may be links.
>>
>> A first try at a solution yields ... oh man, {sigh, deep breath} this,
>> which doesn't work:
>> <https://gist.github.com/uvtc/ca819f08fe7ead81e13055f001a994e3>.
>>
>> Thanks!
>> -- John
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to [email protected]
>> <javascript:>
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> To unsubscribe from this group, send email to
>> [email protected] <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" 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.