我发现Stackoverflow上面临着如此多的资源,但是不幸的是,它们对我没有用,我当然也不知道缺少什么。
要指出我检查过的一些链接,但仍然没有一个可以解决:
http://railscasts.com/episodes/174-pagination-with-ajax
Kaminari with AJAX, unable to paginate
Kaminari Ajax paginate doesn't work
kaminari ajax pagination not updating the paginate
这是我下面做的:
widget_grid.rb
请注意,此表是由Datagrid Gem创造的,并且我的表/列定义如下所示。
class WidgetsGrid
include Datagrid
include Datagrid::Naming
include Datagrid::ColumnI18n
# Scope need for the Table
scope do
Model.includes(:another_model).where({ column A: %w(mango orange) }).order(created_at: :desc)
end
# Filters of the table
filter(:column_A) # Filter by column A
filter(:column_B) # You can filter the table by column B
# Columns of the table
column(:column_A)
column(:column_B)
column(:column_C)
column(:column_D)
end
widget_controller.rb
class WidgetsController < ApplicationController
def dashboard
@grid = WidgetsGrid.new(params[:widgets_grid]) do |value|
value.where(channel_id: @widget.channel).page(params[:page]).per(3)
end
@pagination_params = strip_pagination_params
respond_to :html, :js
end
private
def strip_pagination_params
params.inject({}) do |params_hash, param|
unless param[0]=="controller" || (param[0]=="action")
params_hash[param[0]] = nil
end
params_hash
end
end
end
_insight.html.slim
# By virtue of Datagrid Gem, I render the HTML table and its content and pagination using these two lines below:
= datagrid_table @grid
= paginate @grid.assets, :remote => true, params: @pagination_params
dashboard.html.slim
#insight # note the '#insight' is the same as <div id=insight>
= render partial: "insight_panel"
dashboard.js.erb
$("#insight").html("<%= escape_javascript(render partial: "insight_panel" ) %>");
app / assets / javascripts / dashboard.js.coffee
#= require jquery
#= require jquery_ujs
#= require dashboard_grid
app / assets / javascripts / dashboard_grid.js
$(function () {
$(".pagination").on("click", function () {
$(this).parent().html("Page is loading...");
$.getScript(this.href);
return false;
});
});
当我单击分页链接时,it clears the table and I get "Page is loading..." as in the image below
不会返回我单击的分页的任何页面。 如何将AJAX与Kaminari结合使用进行分页?我不知道缺少什么,这肯定让我感到沮丧。
我还检查了Railscast(我在那发布的第一个链接)上的评论,我看到有人也遇到了这个问题,但是没有解决方法。
我正在使用:
jQuery JavaScript库v1.11.0
用于jQuery的非侵入式脚本适配器,需要jQuery v1.7.0及更高版本。