0

我正在尝试在网页上显示两个序列的内容。其中第一个包含图片的名称,第二个包含图片的 URL。当我尝试遍历这两个列表(使用类似 foreach 的 for 循环)时,问题就开始了——这会导致多个图片名称或多个图片(简而言之,一团糟)。这是我的页面渲染代码示例(使用 将序列转换为矢量(vec(flatten (sequence)))):

    (defn view-output []
  (view-layout
   [:h2 "default images"]


   [:form {:method "post" :action "/"}
     ( for [manganame ["image1" "image2"]]
       [:input {:type "text" :value manganame}]) 

       (for [manga-image["image1URL" "image2URL"]]
         [:img {:src manga-image}[:br] [:br][:br]])  ] ))

此代码将首先显示名称,然后是图片。任何人都可以建议一种将这些值彼此相邻插入的方法,而不是在彼此之上(也许像表格一样的实现)

                               Image_name1
                               Picture1

                             Image_name2
                               Picture2

或者

                    Image_name1      Image_name2
                      Picture1         Picture2

谢谢

4

2 回答 2

2

我刚刚意识到正确的代码是:

(defn view-output []
  (view-layout
    [:h2 "default images"]
    [:form {:method "post" :action "/"}
    (interleave 
      (for [manga-name ["image1" "image2"]]
           [:input {:type "text" :value manga-name}]) 
      (for [manga-image["image1URL" "image2URL"]]
           [:img {:src manga-image}[:br] [:br][:br]]))]))

你想要一个 [:input][:img][:input][:img] 的列表。

我之前的回答是返回 [:input :img][:input :img]:

(defn view-output []
  (view-layout
    [:h2 "default images"]
    [:form {:method "post" :action "/"}
    (for [[manganame manga-img] (map vector ["image1" "image2"] ["image1URL" "image2URL"]))]
      (concat 
         [:input {:type "text" :value manganame}] 
         [:img {:src manga-image} [:br ] [:br ] [:br ]]))]))
于 2012-05-09T09:20:28.313 回答
1
(mapcat (fn [manganame mangaimage]
          [[:input {:type "text" :value manganame}]
           [:img {:src mangaimage}]
           [:br] [:br] [:br]])
        ["image1" "image2"]
        ["image1URL" "image2URL"])

([:input {:type "text", :value "image1"}]
 [:img {:src "image1URL"}]
 [:br] [:br] [:br]
 [:input {:type "text", :value "image2"}]
 [:img {:src "image2URL"}]
 [:br] [:br] [:br])
于 2012-05-10T05:51:57.480 回答