简体   繁体   English

有关在Ruby中打印数组元素的问题

[英]Issue regarding printing of array elements in Ruby

I have an existing rails application. 我有一个现有的Rails应用程序。 In one view file I want to print 2 array elements in a particular manner. 我想在一个视图文件中以特定方式打印2个数组元素。 My two arrays are @itemgroups_array and @all_brands_array,where @all_brands_array is an array of arrays. 我的两个数组是@itemgroups_array和@all_brands_array,其中@all_brands_array是数组的数组。 For each element of @itemgroups_array the corresponding element of @all_brands_array will be printed. 对于@itemgroups_array的每个元素,将打印@all_brands_array的相应元素。 As I just said each element of @all_brands_array is another array and since I want to print the innermost array elements in a same line,I have used join() method. 就像我刚刚说过的,@all_brands_array的每个元素都是另一个数组,由于我想在同一行中打印最里面的数组元素,因此我使用了join()方法。 Upto this its fine. 到此为止。

My code is: 我的代码是:

<% counter = 0 %>
<% @itemgroups_array.each do |ig_name|%>
  <div id="<%=ig_name%>div">
    <%=ig_name%>
    <input type="checkbox" class="check_class" id="<%=ig_name%>c" checked="true">
    <%= @all_brands_array[counter].join(" ")%>

  </div>
  <% counter = counter + 1 %>
<%end%> 

And it is giving output like this: (Conceptually) 它给出的输出是这样的:(概念上)

Conveyor

Jeekay(AD) SKF

Bolt & Nut

Nirlon(AD) FAG SKF(AD)

where I have managed to print Nirlon(AD),FAG,SKF(AD) all in same line by the statement @all_brands_array[counter].join(" "). 我设法通过语句@all_brands_array [counter] .join(“”)在同一行中全部打印Nirlon(AD),FAG,SKF(AD)。 But I want to print it as follows: 但我想将其打印如下:

Conveyor   Jeekay(AD) SKF
Bolt & Nut   Nirlon(AD) FAG SKF(AD)

ie conceptually outer array element and inner array elements in same line just like above line. 也就是说,在概念上,同一行中的外部数组元素和内部数组元素就像上面的线一样。

Please help me about this. 请帮助我。

Thanks in advance! 提前致谢!

My browser generated html for this section is: 我的浏览器为此部分生成的html是:

<div id="persist">
        <div id="Conveyordiv">
            Conveyor
            <input type="checkbox" class="check_class" id="Conveyorc" checked="true">
            Jeekay(AD) SKF

        </div>
        <div id="Bolt &amp; Nutdiv">
            Bolt &amp; Nut
            <input type="checkbox" class="check_class" id="Bolt &amp; Nutc" checked="true">
            Nirlon(AD) FAG SKF(AD)

        </div>

</div>

Float left is the solution. 解决方案是向左浮动。 Provide the div with enough width to the elements and put css property to the div as float:left. 为div提供足够的元素宽度,并将css属性作为float:left放置到div中。

try this: 尝试这个:

<div id="persist">
    <div id="Conveyordiv">
      <div style='float:left'>
        Conveyor
      </div>
      <div style='float:left'>
        <input type="checkbox" class="check_class" id="Conveyorc" checked="true">
        Jeekay(AD) SKF
      </div>

    </div>
    <div id="Bolt &amp; Nutdiv">
      <div style='float:left'>
        Bolt &amp; Nut
      </div>
      <div style='float:left'>
        <input type="checkbox" class="check_class" id="Bolt &amp; Nutc" checked="true">
        Nirlon(AD) FAG SKF(AD)
      </div>
    </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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