模型显示 ajax 在 foundation-zurb 中调用的问题

model reveal issue with ajax call in foundation-zurb

我正尝试在我的应用程序中使用模型显示进行 ajax 调用。

能够在 url 中传递动态 ID,但它在每次点击时仅显示最后一个模型(没有 ajax-call)

我用 ajax-all

尝试了以前的解决方案

但我无法在 javasript 文件中传递动态 ID。

模型显示参考 link:- https://codepen.io/sujayjaju/pen/akAYzP?editors=1010

当前代码在每次显示点击时显示一个模型我也尝试了最后一个解决方案(url 提到)但它不适用于 ajax-调用。

application.js

  $(document).on('open.zf.reveal', "#exampleModal1", function (e) {
var $modal = $(this);
var ajax_url = $modal.data("ajax-url");
if (ajax_url) {
  $modal.html("Now Loading: "+ajax_url);
  $.ajax(ajax_url).done(function (response) {
    $modal.html(response);
  });
}
});

index.html.erb

<% @project.project_sites.where(submission_status: true).order("created_at desc").each do |project_site| %>
  <tr>
      <td><%= project_site.user.name %></td>

       <td>
        <div class="full reveal" id="exampleModal1" data-reveal data-ajax-url="http://0.0.0.0:3000/project_sites/<%= project_site.id %>/attendances/">

         <button class="close-button" data-close aria-label="Close modal" type="button">
         <span aria-hidden="true">&times;</span>
         </button>
         </div>

         <p><button class="button small warning button-margin-top fi-eye" data-open="exampleModal1"> View</button></p>

       </td>
    </tr>
<%end %>

你可以从我的申请中参考这个particular commit很好。

Javascript代码

$(document).on('turbolinks:load', function() {
  $(document).foundation();
  $('[data-open="ajax-reveal"]').on('click', function(e) {
    e.preventDefault();
    $.ajax({
      url: $(this).data('url'),
      success: function(result) {
        $(".reveal-content").html(result);
      },
      error: function(result) {
        $(".reveal-content").html('Error loading content. Please close popup and retry.');
      }
    });
  });
});

HAML 代码

          #ajax-reveal.reveal{'data-reveal': true, 'aria-labelledby':"modalTitle", 'aria-hidden': true, role: "dialog"}
            .reveal-content
            %button.close-button{'aria-label': "Close modal", type: :button, data: {close: true}}
              %span{'aria-hidden': true} &times;

我的链接是这样的

<% @project.project_sites.where(submission_status: true).order("created_at desc").each do |project_site| %>
  <%- 'YOUR OTHER CODE' %>
  <%= link_to 'View', 'javascript:void(0)', class: 'button OTHER-CLASSES', data: {open: 'ajax-reveal', url: project_site_path(project_site)} %>
<% end %>

我用来显示的一些方便的帮手是

  def close_reveal_button(text = 'Cancel')
    content_tag(:div, text, class: 'button warning', data: {close: ''})
  end
  def link_to_reveal(content, url, classes)
    link_to content, 'javascript:void(0)', class: classes, data: {open: 'ajax-reveal', url: url}
  end

您可以推荐更多帮手here

刚刚找到了解决这个问题的简单方法:-(使用内部 jquery)

index.html.erb

<% @project.project_sites.where(submission_status: true).order("created_at desc").each do |project_site| %>
  <tr>
       <td>
           <% site_modal_id = "site_modal_#{project_site.id}" %>

                  <div class="full reveal" id="<%= site_model_id %>" data-reveal data-ajax-url="http://0.0.0.0:3000/project_sites/<%= project_site.id %>/attendances/">

              <button class="close-button" data-close aria-label="Close modal" type="button">
                <span aria-hidden="true">&times;</span>
              </button>
           </div>

            <p><button class="button small warning button-margin-top fi-eye" data-open="<%= site_modal_id %>"> View</button></p>

       </td>
    </tr>

<script>
$(document).on('turbolinks:load', function() {
  $(function(){ $(document).foundation(); });

  $(document).on('open.zf.reveal', "#<%= site_modal_id %>", function (e) {
    var $modal = $(this);
    var ajax_url = $modal.data("ajax-url");
    if (ajax_url) {
    $modal.html("Now Loading: "+ajax_url);
    $.ajax(ajax_url).done(function (response) {
      $modal.html(response);
    });
    }
  });

});
</script>
<%end %>