如果值为true,则显示复选框,否则隐藏delete

时间:2018-02-21 08:02:57

标签: jquery ajax ruby-on-rails-4

Before delection i have index.js.erb like this

$(".ajax-table tbody").html($("<%=j render(partial:'index_column') %>"));

$('.row .pagination').html("<%=j page_entries_info resources %>");
$('.paginate-container').html("<%=j paginate(resources, remote: true) %>");

bindChecks();
$("#all_destroy").prop("checked", false);
syncDestroyButton();
<% if resources.total_pages < resources.current_page %>
  $(".pagination .page:last a[data-remote=true]").trigger("click");
<% end %>

$("input[type=checkbox]").each(function() {
  var MediaLibrary = checkMediaLibraries[this.name];
  if (MediaLibrary != undefined) {
    $(this).prop("checked", MediaLibrary);
  }
});

我的index.html.erb

<%= render "title" %>

<%= render "index_header" %>

<table class="table table-hover ajax-table">
  <thead>
    <tr>
      <th>
        <%= check_box 'all', 'destroy', {:checked => false} %>
      </th>
      <% columns.each do |column| %>
        <% if column == :media %>
          <th></th>
        <% else %>
          <th><%= link_to_sort column %></th>
        <% end %>
      <% end %>
      <th></th>
    </tr>
  </thead>

  <tbody>
    <%= render partial: "index_column" %>  
  </tbody>
</table>

<% @ajax_paginate = true %>
<%= render "index_footer" %>

<%= table_selectable %>

<%= javascript_tag do %>
  // ページネーション遷移ごとにチェックしたユーザかどうか記録するグローバル変数
  checkMediaLibraries = {};

  function syncDestroyButton() {
    if (checkedMediaLibraryCount() != 0) {
      $("#btn-bulk-destroy").removeClass("disabled");
    } else {
      $("#btn-bulk-destroy").addClass("disabled");
    }
  }

  function syncCheckMediaLibraries() {
    $("td input[type=checkbox]").each(function() {
      checkMediaLibraries[this.name] = $(this).is(":checked");
    });
  }

  function checkedMediaLibraryCount() {
    syncCheckMediaLibraries();
    var count = 0;
    for (var k in checkMediaLibraries) {
      if (checkMediaLibraries[k]) count++;
    }
    return count;
  }

  function bindChecks() {
    // 個別チェック
    $(".destroy-checkbox").on("click", function(e) {
      if (e.target.tagName != 'INPUT') {
        $(this).children("input:checkbox").click();
      }
      e.stopPropagation();
      syncDestroyButton();
    });
  }

  function bulkDestroy() {
     checkMediaLibraries["_method"] = "DELETE";
     $.ajax({
       type: "POST",
       url: "<%= bulk_destroy_admin_media_libraries_path(site_id: @site.identifier) %>",
       data: checkMediaLibraries,
       dataType: "json",
       success: function(data) {
         $(".pagination .page.active a[data-remote=true]").trigger("click");
         checkMediaLibraries = {}
         Notify.success(data.message);
       },
       error: function(xhr) {
         Notify.error(xhr.responseJSON.message);
       }
     });
     return false;
   }

  $(function() {
    // 全てチェックor外す
    $("#all_destroy").click(function() {
      $(".destroy-checkbox input:checkbox").prop("checked", $(this).is(":checked"));
      syncDestroyButton();
    });

    $("#btn-bulk-destroy")
      .click(bulkDestroy)
      .popConfirm(Confirm.setting(I18n.t("js.crud.action.bulk_destroy", {count: 0})))
      .click(function() {
        $("#popover_content_area").text(I18n.t("js.crud.action.bulk_destroy", {count: checkedMediaLibraryCount()}));
      });

    bindChecks();
    syncDestroyButton();

    $(document).on("click", "a[data-remote=true]", syncCheckMediaLibraries);
  });
<% end %>

我选中了一个复选框并单击删除按钮,显示隐藏的复选框,但是如果我刷新页面就可以了。所以我不知道出了什么问题。我不想使用location.reload。 这是我在_index_column.html.erb

中显示数据的方式
  

&lt;%if if can?(:destroy,resource)%&gt; &LT;%=   f.hidden_​​field:destroy_action,:value =&gt; false%&gt; &lt;%= f.check_box   :destroy_action,{},true,false%&gt; &lt;%end%&gt; &lt;%columns.each do   |栏| %GT; &lt;%= column_html资源,列%&gt; &lt;%end%&gt; &lt;%(index_actions - [:show])。每个do | action | %GT; &LT;%=   link_to_action操作,资源%&gt; &lt;%end%&gt;

0 个答案:

没有答案