简体   繁体   中英

Ruby/Rails/HTML/HAML - create a two column table with first column cells spanning dynamic number of rows

I am trying to create an HTML table in HAML that has a first column where each cell spans a dynamic number of rows based on the number of values in an Array which will be split across multiple cells in the second column. So for example, if Array.size == 2, then the first cell in the first column will have rowspan=2 and the second column will have Array[0] in the first row and Array[1] in the second row and the column 1 value will span both rows. The HAML code I wrote to do this is as follows:

- @array1.each do |item|
    %tr
    %td{:rowspan => "#{item.array.size}"}= time_tag(item.created_at)
    - item.array.each do |item|
      %td= "#{item.name}" 
      %tr

The issue I am having is that HAML automatically inserts one set of row tags after each loop through the second block which results in the second item.name ending up in the first column of the second row instead of in the second column of the second row. How can I fix my code to make the table work as I described it?

I don't know if I understand you but I think it could work with the following code:

%table
- @products.each do |item|
  %tr
    %td{:rowspan => "#{item.array.size}"}= "#{item.name}"
    %td
      - item.array.each do |item2|
        %tr
          %td= "#{item2.name}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM