Butter Beer
Butter Beer

Reputation: 1150

Sorting table by both column and row Rails

Here, I want to set up a weekly planner where I will later insert plans into.

I have the columns sorted by the days of the week.(Monday, Tuesday...) Now, I want the row to be sorted by the time.

However, with my code below. For every column, it loops through the same number of times as the number of items in my time slot.

Would appreciate it if you could give me some guidance on how I should go about fixing it such that each timeslot only appears once throughout the whole table.

The codes

<table class="Time Table">
  <tr>
    <th><%= "Time" %></th>
    <th><%= "Monday" %></th>
    <th><%= "Tuesday" %></th>
    <th><%= "Wednesday" %></th>
    <th><%= "Thursday" %></th>
    <th><%= "Friday" %></th>
    <th><%= "Saturday" %></th>

 </tr>
  <% @time_slots.each do |time_slot| %>
    <tr>
      <td></td>
        <td><% if time_slot.dayOfWeek = "Monday" %><%= time_slot.startTime %><% end %></td>
        <td><% if time_slot.dayOfWeek = "Tuesday" %><%= time_slot.startTime %><% end %></td>
    <td><% if time_slot.dayOfWeek = "Wednesday" %><%= time_slot.startTime %><% end %></td>
    <td><% if time_slot.dayOfWeek = "Thursday" %><%= time_slot.startTime %><% end %></td>
    <td><% if time_slot.dayOfWeek = "Friday" %><%= time_slot.startTime %><% end %></td>
    <td><% if time_slot.dayOfWeek = "Saturday" %><%= time_slot.startTime %><% end %></td>
    </tr>
  <% end %>

Upvotes: 1

Views: 92

Answers (1)

Brad Werth
Brad Werth

Reputation: 17647

OK, you've just got it flipped around... You need something like this:

<% days = %w[ Monday Tuesday Wednesday Thursday Friday Saturday] %>

<% days.each do |day| %>
   <td>
   <% @time_slots.select {|time_slot| time_slot.dayOfWeek == day }.each do |time_slot| %>
     <%= time_slot.startTime %><br/>
   <% end %>
   </td>
<% end %>

Hope this helps, good luck.

Upvotes: 2

Related Questions