Clojure use (for) with hiccups and noir

I use clojure and hiccup (with noir) and I have this code:

(defn dataframe [id] (db/db-to-data id)) (defpartial drop-downs [nms] (for [nm (keys nms)] (drop-down nm (get nms nm))[:br]) (submit-button "Refresh") ) (defpage "/dataset/table/:id" {:keys [id]} (common/layout (form-to [:post (format "/dataset/table/%s" id)] (drop-downs {"alessio" [:col0], "test" [:col1]}) ) (html-table (dataframe id)))) 

My problem is related to:

 (for [nm (keys nms)] (drop-down nm (get nms nm))[:br]) 

I want to have several options in my form. The line above does this, but for some reason it does not consider [: br], so it does not break the line. However, if I do this:

 (form-to [:post (format "/dataset/table/%s" id)] (drop-down "Test1" "1")[:br] (drop-down "Test2" "2")[:br] ) 

The [: br] tag works. I believe that this is due to how the macro works (for), but I could not understand the reason and how to fix it.

EDIT

As advised, I refused to use. The final result is below (this is Joost's answer with a little mod):

 (mapcat #(vector (drop-down % (nms %)) [:br]) (keys nms)) 
+4
source share
3 answers

This code does not even compile; for takes exactly two arguments.

If you want to place two elements at once in the sequence returned by for , put them in a vector and unpack them later.

Although I personally prefer mapcat for these situations. Sort of:

 (into (form-to [:post (format "/dataset/table/%s" id)]) (mapcat #(vector (drop-down % (nms %)) [:br]) nms)) 
+5
source

I have put together something that doesn’t use Noir or hiccup, but might help you in a better direction. It uses mapcat instead:

  (let [nms {"alessio" [:col0], "test" [:col1]}] (mapcat (fn [mapentry] [[:dropdown (first mapentry) (second mapentry)] [:br]]) nms)) ;;=> ([:dropdown "alessio" [:col0]] [:br] [:dropdown "test" [:col1]] [:br]) 
+1
source

Hiccup automatically expands seqs / lists (not vectors!) For you, so you can write this as:

 (for [[kv] nms] (list (drop-down kv) [:br]))) 
0
source

Source: https://habr.com/ru/post/1380892/


All Articles