在 Reagent 中使用 colspan 渲染 table 元素

Render table element with colspan in Reagent

我在试剂中有一个 table 并设置了一个 table header = 2 的 col-span。

 (defn my-table []
  [:table.table.table-striped.table-bordered
   [:thead
    [:tr
     [:th "Col1"]
     [:th "Col2"]
     (doall (for [id @ids]
          ^{:key id}
          [:th {:data-id (id :id)
                :col-span "2"}
           (str (get id :name))]))]]
   [:tbody
    (doall (for [i (range (count @table-items))]
         (let [table-item (cursor table-items [i])]
           ^{:key i}
           [item-row table-item])))]])

但是在每一行中,请参见下面的 item-row,我正在遍历数据结构以获取该行中拆分列的数据,但遇到了问题。如果

我可以让数据正确呈现
[:div [:td [:span (:id id)]]
             [:td [:span (:name id)]]]

我知道是 incorrect/invalid html.

 (defn item-row [item]
    (fn []
      [:tr
       [:td [:span (:key1 @item)]]
       [:td [:span (:key2 @item)]]
       (doall (for [id @ids]
            ^{:key id}
            [[:td [:span (:x id)]]
             [:td [:span (:y id)]]]))]))

有人知道如何在 for 循环中渲染两个 :td 元素吗?我的想法是将两个 :td 元素包装在一个向量中,让试剂处理渲染拆分列。

 (doall (for [id @ids]
            ^{:key id}
            [[:td [:span (:x id)]]
             [:td [:span (:y id)]]])

我也收到以下错误: core.cljs:4793 Uncaught Error: No item [:td "."] in vector of length 2 at Object.cljs$core$vector_index_out_of_bounds [as vector_index_out_of_bounds]

[:td [:span (:y id)]]

相关

你可以利用你只是返回数据这一事实,所以一种方法是用前两列构建 :tr 向量的初始内容,然后添加其余的使用 into 中的 :td 个元素。这是我认为可以解决您的问题的示例:

cljs.user=> (def ids [{:x 1 :y 2} {:x 3 :y 4}])
#'cljs.user/ids
cljs.user=> (let [tr [:tr 
       #_=>           [:td "first col"]
       #_=>           [:td "second col"]]]
       #_=>   (reduce (fn [acc id]
       #_=>             (into acc
       #_=>                   [[:td [:span (:x id)]]
       #_=>                    [:td [:span (:y id)]]]))
       #_=>           tr
       #_=>           ids))
[:tr [:td "first col"] [:td "second col"] [:td [:span 1]] [:td [:span 2]] [:td [:span 3]] [:td [:span 4]]]
cljs.user=>