Rails动态collection_select仅过滤大约50%的时间

时间:2019-01-09 00:13:23

标签: javascript jquery ruby-on-rails

我已经实现了动态选择,用户可以在一个选择中选择他们正在处理的项目,然后将出现带有多个“子任务”的下拉菜单。
此方法有效,但仅占大约50%的时间,有时第二个下拉菜单包含所有带有子任务的项目,其他时间子任务将根据所选项目填充。

这是我的Task.js函数:


    function showForm() {
      const btn = document.getElementById('new-task-form')
      btn.style.display == "block" ? btn.style.display = "none" : btn.style.display = "block"
    }

    jQuery(function() {
      let subtasks;
      subtasks = $('#task_subproject_id').html();
      return $('#task_project_id').change(function(){
        let project, optons;
        project = $('#task_project_id :selected').text();
        options = $(subtasks).filter("optgroup[label='" + project + "']").html();
        if(options) {
          return $('#task_subproject_id').html(options);
        } else {
          return $('#task_subproject_id').empty();
        }
      });
    });

这是显示集合的新部分:


    <%= form_for(@task, method: :post, remote: true, html: {class: "form-group"}) do |f| %>
      <%= f.label "Describe the task"%>
      <%= f.text_field :comments, id: "task-comments", class: "form-control" %>
      <br />
      <%= f.label "Time in minutes" %>
      <%= f.text_field :time, class: "form-control" %>
      <%= f.label "Project" %>
      <%= f.collection_select :project_id, Project.order(:name), :id, :name,
          {prompt: "Select a project"}, {class: "form-control"} %>
      <br />
      <%= f.grouped_collection_select(:subproject_id, Project.order(:name), :subprojects, :name,
          :id, :name, {prompt: "Select a subtask" },{class: "form-control"}) %>
    <br / />
      <%= f.label "Date" %>
      <%= f.date_select :day,  {class: "form-control"}%>

      <br  />
      <%= f.label "Collaborators" %>
      <ul class="list-group collaborators">
        <%= f.collection_check_boxes(:user_ids, User.where.not(id: current_user.id), :id, :email_or_user_name) do |box| %>
        <li class="list-group-item"> <%= box.check_box + box.text%> </li>

        <% end %>
        </ul>
      <%= f.submit "Add Task", class: 'btn btn-primary' %>
    <% end %>

同样,这种方法大约有50%的时间有效。我已经在多种浏览器中尝试过; Safari,Chrome,IE,Opera似乎不是浏览器问题,当它无法正常工作时,没有控制台错误。

0 个答案:

没有答案