【问题标题】:Displaying array in td tag in Haml在 Haml 的 td 标签中显示数组
【发布时间】:2015-11-12 06:32:09
【问题描述】:

我有以下 div,

 - abc.each do |gg, arr = []|
  - gg.items.each {|def| arr.push(def.values) }

  = modal(attributes: {id: 'some-modal', class: 'modal-wide'}, legacy: true) do
    = header('Item Details', attributes: {id: 'some-modal-header'})

    #graph
      - y_axis = array_values(arr)
      %table{data: {chart: 'line'}, class: 'table-hover'}
        %tbody
          %tr{data: { y_values: y_axis, x_values: [3, 10, 12, 20, 32, 35, 50, 52, 90]}}
            - y_axis.each do |y|
              %td= y

    = actions do
      %span= 'hello'
      %button.btn(data-dismiss='modal' aria-hidden='true')= 'close'

我不知道我在这里做错了什么。当我用这些值对 y_axis 进行硬编码时,它工作正常。当我使用上面的代码时,会跳过循环并转到“动作”。有人能指点一下方向吗?

【问题讨论】:

  • 你好。你已经实现了上面的代码;它已经写好了。您能否给出所有变量的示例值,以及您希望使用 Haml 实现的最终 HTML(不是 Haml)?

标签: javascript html ruby haml


【解决方案1】:

不清楚你想要什么,但正如我从你的代码中理解的那样,你想为 td 创建一个循环:

%table{data: {chart: 'line'}, class: 'table-hover'}
  %tbody
    %tr{data: { y_values: arr, x_values: another_array}}
      - arr.each do |ar|
        %td
          = ar # this will print value of ar from loop of arr 

让我知道这是否是您正在寻找的内容,或者向我提供更多信息,以便我可以相应地修改我的答案

【讨论】:

  • @vanga :如果您向我们提供数组和另一个数组的对象内容,将会更有帮助。以及您希望的简单 HTML 语法。
  • 我很抱歉造成混乱。我已经更新说明我正在尝试做的事情。谢谢
【解决方案2】:

Haml 允许您轻松使用 ruby​​ 迭代器和循环:

- arr.each do |element|
   %td= element

请记住以each 开头,而不是=,而不是end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    相关资源
    最近更新 更多