turbolinks和ajax的问题

时间:2014-05-22 13:33:39

标签: jquery ruby-on-rails ajax turbolinks

我将以下内容作为ajax调用:

$.ajax(
        type: "GET"
        url: "/admin/requests"
        format: 'js'
        dataType: "script"
        data:
            type_of_function: type_of_function
            agents: agent
            payment_status: payment_status_array
            status: status_array
            priority: priority_array
            due_by_date: due_by_array
            services: services
            sources: sources
            created_at: created_at
            leads: leads
            widgets: widgets
            customers: customers
            types: types
            vendors: vendors
            groups: groups
            payment_modes: payment_modes
            delete_request: delete_request_array_val
            spam_request: spam_request_array_val
            pickup_request: pickup_request_array_val
            close_request: close_request_array_val
            assign_agent_request: assign_agent_request_val
            assign_agent_id: assign_agent_id
            bulk_request: bulk_request_array_val
            bulk_request_agent: bulk_request_agent
            bulk_request_ps: bulk_request_ps
            bulk_request_status: bulk_request_status
            bulk_request_priority: bulk_request_priority
            bulk_request_source: bulk_request_source
            bulk_request_type: bulk_request_type
            bulk_request_widget: bulk_request_widget
            bulk_request_lead: bulk_request_lead
            bulk_request_reminder: bulk_request_reminder
    )

现在以下是我正在使用的index.js.erb

$(".request_table_span").html('<%= escape_javascript(render "request_table",requests: @requests,model_class: "Request")%>' );

$('#paginator').html('<%= escape_javascript(paginate(@requests, :remote => true)) %>');

$(".search-free-chosen-select").select2({
minimumResultsForSearch: -1,
width: "150px"
});

$(".chosen-select").select2(width: "150px");

checkbox();

这是checkbox()函数:

checkbox = ->
    $("#request_table_checkbox_main").change ->
        if $("#request_table_checkbox_main").is(":checked")
            $(".request_table_checkbox").prop('checked', true)
        else
            $(".request_table_checkbox").prop('checked', false)

read = ->
    checkbox();

$(document).ready read
$(document).on "page:load", read

这是视图文件。我使用kaminari进行分页。

%table#datatables-requests.table.table-striped

  %thead
    %tr
      %th
        = check_box_tag '',1,false,id: "request_table_checkbox_main"
      %th Service
      %th Vendor
      %th Status
      %th Payment Status
      %th Due on
      %th= t '.actions', :default => t("helpers.actions")
      %th
  %tbody
    - requests.each do |req|
      %tr
        %td
          = check_box_tag '',req.id,false,class: "request_table_checkbox"
        %td
          = req.service.name
          %br
            %span{:style => "color:blue;"}= link_to "Create SOD/Invoice", new_admin_invoice_path(req: req.id)
        %td= req.vendor_service.name

        %td= select_tag "", options_for_select(Admin::FilterStatus.all.collect{ |u| [u.name, u.id]}, :selected=>req.admin_filter_status.id),class: "search-free-chosen-select status_column_val"
        %td= select_tag "", options_for_select(Admin::FilterPaymentStatus.all.collect{ |u| [u.name, u.id]}, :selected=>req.admin_filter_payment_status.id),class: "search-free-chosen-select payment_status_column_val"

        %td= l req.due_on.to_date
        %td
          -if req.due_on < DateTime.now 
            %span.subject_style.overdue Overdue 
        %td
          = link_to "View", admin_request_path(req), :class => 'btn btn-primary'
          = link_to t('.edit', :default => t("helpers.links.edit")),edit_admin_request_path(req), :class => 'btn btn-mini'
          = link_to t('.destroy', :default => t("helpers.links.destroy")),admin_request_path(req),:method => :delete,:data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },:class => 'btn btn-mini btn-danger'
        %td 
          = render "last_column_in_table", req: req
#paginator
  = paginate requests,remote: true
%br

我正在为基于ajax的页面加载制作Kaminari。如果我使用index.js.erb中的当前代码,它将无法工作。此外,如果我删除checkbox它工作正常。为什么会这样?


更新

$(".request_table_span").html('<%= escape_javascript(render "request_table",requests: @requests,model_class: "Request")%>' );

$('#paginator').html('<%= escape_javascript(paginate(@requests, :remote => true)) %>');

$("#request_table_checkbox_main").on('change', function() {
return $(".request_table_checkbox").prop('checked', $("#request_table_checkbox_main").is(":checked"));});

$(".search-free-chosen-select").select2({
minimumResultsForSearch: -1,
width: "150px"
});

$(".chosen-select").select2({
    minimumResultsForSearch: -1,
    width: "150px"
});

在上面的代码中,(“。search-free-chosen-select”)之上的所有内容都运行良好。这太奇怪了。

@MrYoshiji你对javascript工作的改进。但是,如果我在index.js.erb中调用该函数它似乎无效。但是,如果我真的将复选框部分单独粘贴在那里,它就可以了。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法吗?

window.sync_checkboxes = ->
  $("#request_table_checkbox_main").on 'change', ->
    $(".request_table_checkbox").prop('checked', $("#request_table_checkbox_main").is(":checked") )

window.initialize_page = ->
  sync_checkboxes()
  true

$(document).ready initialize_page()
$(document).on 'page:load', initialize_page()