我正在使用 will_paginate 成功翻阅记录。我还通过 jQuery 使用 AJAX 实时搜索来更新我的结果 div。到目前为止没有问题。我遇到的问题是在尝试对这些实时搜索结果进行分页时。我只是得到“页面正在加载......”而没有 div 更新。我错过了一些基本的东西吗?我相信我可能还需要通过 jQuery 以某种方式绑定分页链接的点击事件。
# index.html.erb
<form id="searchform" accept-charset="utf-8" method="get" action="/search">
Search: <input id="search" name="search" type="text" autocomplete="off" title="Search location, company, description..." />
<%= image_tag("spinner.gif", :id => "spinner", :style =>"display: none;" ) %>
</form>
# JobsController#search
def search
if params[:search].nil?
@jobs = Job.paginate :page => params[:page], :order => "created_at desc"
elsif params[:search] and request.xhr?
@jobs = Job.search params[:search], params[:page]
end
render :partial => "jobs", :layout => false, :locals => { :jobs => @jobs }
end
# Job#search
def self.search(search, page)
logger.debug "Job.paginate #{search}, #{page}"
paginate :per_page => @@per_page, :page => page,
:conditions => ["description LIKE ? or title LIKE ? or company LIKE ?",
"%#{search}%", "%#{search}%", "%#{search}%"],
:order => 'created_at DESC'
end
# search.js
$(document).ready(function(){
$("#search").keyup(function() {
$("#spinner").show(); // show the spinner
var form = $(this).parents("form"); // grab the form wrapping the search bar.
var url = form.attr("action"); // grab the URL from the form's action value.
var formData = form.serialize(); // grab the data in the form
$.get(url, formData, function(html) { // perform an AJAX get, the trailing function is what happens on successful get.
$("#spinner").hide(); // hide the spinner
$("#jobs").html(html); // replace the "results" div with the result of action taken
});
});
});