vgrocha / hiccup-table

From clojure data structures, create html table using hiccup representation
14 stars 2 forks source link

Cell with a Vector #3

Closed Quasaur closed 8 years ago

Quasaur commented 8 years ago

Here's an example row from my Neo4j database:

[0 [Event Calendar] Precessional Era of Leo Earth -105 2095 -11080 -8880 The Precessional Era of Leo (2,200 years)]

Notice that the 2nd cell ("[event calendar]") is itself a vector.

Currently your library only gets the 2nd item ("Calendar").

Could you please make your library include both "Event" and "Calendar" in the cell or show me how to?

Thanks!

Quasaur commented 8 years ago

In my application (which uses a Neo4j database) I only download vectors, not maps. I know that the built-in Clojure function (get-in) processes my data quite nicely, but am not sure where in your lib to put it.

vgrocha commented 8 years ago

Hey Mitchell! That's because the contents of the hiccup table are treated as hiccup structures. That means if you have a vector, the first element will be parsed as the html element, the second, if it's a map, into attributes and the rest as content, c.f Hiccup syntax. In that sense

["event" "Calendar"]

is parsed to

<event>
  Calendar
</event>

Does that make sense?

For this to work, you need to join the elements from your vector. For example, using the most recent v0.2.0:

=> (to-table1d '(["row 1" ["two" "fields"]]
                 ["row 2" ["two" "fields"]])
               [0 "Header 1" 1 "Header 2"]
               {:data-value-transform
                (fn [key val]
                  (case key 1
                        (clojure.string/join " " val)
                        val))})

;=> [:table nil
     [:thead nil
      [:tr nil ([:th nil "Header 1"] [:th nil "Header 2"])]]
     [:tbody nil
      ([:tr nil ([:td nil "row 1"] [:td nil "two fields"])]
       [:tr nil ([:td nil "row 2"] [:td nil "two fields"])])]]