Grouped_select,如何显示之前选择的项目 and/or 类别

Grouped_select, how to display previously selected item and/or category

在预订表格中,我试图显示可用 rooms 及其 room_category 是否在特定时间段内可用。创建新预订时一切正常。

问题

当我想编辑预订表格(更具体地说是时间跨度)时,表格下拉列表清空并重新建立起来。出现的问题是先前选择的 room 及其 room_category 在可用时未显示为 selected。

问题

如何:

代码

预订单

<div class="col col-sm-4">
        <%= f.input :arrival,
        as: :string,
        label:false,
        placeholder: "From",
        wrapper_html: { class: "inline_field_wrapper" },
        input_html:{ id: "start_date"} %>
      </div>
      <div class="col col-sm-4">
        <%= f.input :departure,
        as: :string,
        label:false,
        placeholder: "From",
        wrapper_html: { class: "inline_field_wrapper" },
        input_html:{ id: "end_date"} %>
      </div>

      <div class="col col-sm-4">
        <%= f.input :room_id, collection: @room_categories.order(:name), as: :grouped_select, group_method: :rooms, label:false %>
      </div>

JS

const checkIn = document.querySelector('#start_date');
const checkOut = document.querySelector('#end_date');


  const checkInAndOut = [checkIn, checkOut];

  checkInAndOut.forEach((item) => {
    item.addEventListener('change', (event) => {
      if ((checkIn.value.length > 0) && (checkOut.value.length > 0)){
      checkAvailability();
    }
  })
});

    function checkAvailability(){

      Rails.ajax({
        url: "<%= rooms_availability_hotel_path(@hotel) %>" ,
        dataType: 'json',
        type: "POST",
        data: `arrival=${start_date.value}&departure=${end_date.value}`,
        success: function(data) {
          console.log(data);
        },
        error: function(response) {
          console.log(response);
        }
      });
    };

控制器检查可用性

  def rooms_availability
    hotel = Hotel.includes(:rooms).find(params[:id])
    arrival = Date.parse(room_params[:arrival])
    departure = Date.parse(room_params[:departure])
    time_span = arrival..departure
    @unavailable_rooms = Room.joins(:reservations).where(reservations: {hotel: hotel}).where("reservations.arrival <= ? AND ? >= reservations.departure", arrival, departure).distinct + (Room.joins(:reservations).where(reservations: {hotel: hotel}).where("reservations.arrival <= ? AND ? <= reservations.departure", arrival, departure).distinct)
    @hotel_cats = hotel.room_categories
    @hotel_accos = Room.where(room_category: @hotel_cats)
    @rooms = @hotel_accos - @unavailable_rooms
    respond_to do |format|
      format.js
    end
  end

hotels/rooms_availability.js.erb

var selectList = document.getElementById('reservation_room_id')
function empty() {
  selectList.innerHTML = "";
}

empty();

<% unless @rooms.empty? %>
      <% @hotel_cats.each do |cat|%>
        selectList.insertAdjacentHTML('beforeend', '<optgroup label=<%= cat.name %>>');
        <% cat.rooms.each do |room|%>
          <% if @rooms.include? room %>
            selectList.insertAdjacentHTML('beforeend', '<option value="<%= room.id %>"><%= room.name %></option>');
          <% end %>
        <% end %>
        selectList.insertAdjacentHTML('beforeend', '<optgroup>');
      <% end %>
    <% end %>

select 之前选择的房间,如果它可用于新 selected 日期

<% unless @rooms.empty? %>
  <% @hotel_cats.each do |cat|%>
    selectList.insertAdjacentHTML('beforeend', '<optgroup label=<%= cat.name %>>');
    <% cat.rooms.each do |room|%>
      <% if @rooms.include? room %>
        selectList.insertAdjacentHTML(
          'beforeend',
          <%= "<option value='#{room.id}' #{@previous_room.id == room.id ? "selected" : ""}>#{room.name}</option>" %>
        );  
      <% end %>
    <% end %>
    selectList.insertAdjacentHTML('beforeend', '<optgroup>');
  <% end %>
<% end %>

或者,如果房间不可用,如何从之前选择的room_category

中自动select一个房间
<% unless @rooms.empty? %>
  <%
  is_previous_room_available = @rooms.include? @previous_room
  set_selected = false
  %>
  <% @hotel_cats.each do |cat|%>
    selectList.insertAdjacentHTML('beforeend', '<optgroup label=<%= cat.name %>>');
    <% cat.rooms.each do |room|%>
      <% if @rooms.include? room %>
        <%
        if (is_previous_room_available)
          # When previous room is available
          %>
          selectList.insertAdjacentHTML(
            'beforeend',
            <%= "<option value='#{room.id}' #{@previous_room.id == room.id ? "selected" : ""}>#{room.name}</option>" %>
          );
          <%
        elsif (is_previous_room_available.! && set_selected.! && @previous_cat.id == cat.id)
          # When previous room not available and this room is first room of previous category
          %>
          selectList.insertAdjacentHTML(
            'beforeend',
            <%= "<option value='#{room.id}' selected>#{room.name}</option>" %>
          );
          <%
          # set first room is selected
          set_selected = true
        else
          %>
          selectList.insertAdjacentHTML(
            'beforeend',
            <%= "<option value='#{room.id}'>#{room.name}</option>" %>
          );
          <%
        end
        %>
      <% end %>
    <% end %>
    selectList.insertAdjacentHTML('beforeend', '<optgroup>');
  <% end %>
<% end %>