N
N
Nikita2016-12-02 00:18:39
Ruby on Rails
Nikita, 2016-12-02 00:18:39

How to display a sorted array by columns in a view?

Good day!
There is an array sorted by some data, let's call it @array . It has, for example, 47 elements. You need to display the contents of the array in the view by columns, not by rows using bootstrap. If output like this:

- @array.each do |x|
  .col-sm-3.col-md-3.col-lg-3
    = x

Then the sort result is displayed in rows, but I would like to implement this in columns. The result should be displayed in 4 columns (always, statically, if there is not enough data for 4 columns, then output as much as it is enough).
So it is not necessary:
2868e74e01cf4c0ebd32c8154c8d5ac1.png
​​So it is necessary:
e2e3a1e576ac437abc11dfe70865ed7a.png
​​Help please, who implemented similar.
The solution was made as follows:
/ Desktop list.
.hidden-xs.text-center
  - @array.in_groups(4, false).each do |array|
    .col-sm-3.col-md-3.col-lg-3
      - array.each do |value|
        .row
          = link_to value[:name], make_path(value[:id])

/ Mobile list.
.visible-xs.text-center
  - @array.in_groups(2, false).each do |array|
    .col-xs-6
      - array.each do |value|
        .row
          = link_to value[:name], make_path(value[:id])

Answer the question

In order to leave comments, you need to log in

5 answer(s)
N
N. Bekseitov, 2016-12-02
@niks123123

Use in_groups

A
Alexander, 2016-12-02
@alexmixaylov

there is no way you can do it like this,
you need to write crutches, for example,
measure the number of array elements,
divide this number by 4 and write down the conditions for displaying elements

Вайладион Гогназдиак, 2016-12-02
@etspring

Самое кошерное - транспонировать массив.

I
iBird Rose, 2016-12-02
@iiiBird Куратор тега Bootstrap

https://jsfiddle.net/rwy5u2ox/1/

I
Ivan Nesterovich, 2016-12-08
@vanderv

@array.in_groups_of((@array.size.to_f/2).ceil, false) do |array|

end

Разобьет массив на 2 столбца

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question