Roll Your Own HTML in Clerk

The clerk/table component automatically limits itself to only showing 20 results. Other presentation components, especially text, have configurable elision behaviors, but it doesn’t apply to the table.

After searching, guessing at ways to do it, and even asking in conference talks, I finally realized that we can render our own HTML with clerk/html and hiccup. That HTML is not limited to any size, and building a table is easy. We used to do it all the time.

I wrote my own simple function to render a table, and I used that instead of the clerk/table. It takes the same parameters I was already using.

^{:nextjournal.clerk/visibility {:code :hide :result :hide}}
(ns sample
  {:nextjournal.clerk/visibility {:code :fold :result :show}}
   [nextjournal.clerk :as clerk]))

^{:nextjournal.clerk/visibility {:code :hide :result :hide}}
(defn my-table
  "display a simple table in html.
  :head is the sequence of head labels.
  :rows is a sequence of sequences.
  :limit is the max to display of the rows. (default 100)"
  (clerk/html [:table
                 (for [h (:head params)] [:th h])]]
                (for [r (take (or (:limit params) 100) (:rows params))]
                   (for [c r]
                     [:td c])])]]))

  {:head ["x" "y"]
   :limit 100
   :rows [[1 2]
          [3 4]]})

Update 2023-11-27

As of the 0.15.957 release, clerk tables have a ::clerk/page-size parameter, so I use that now instead of the code above.

Filed Under: Clojure Code