3
我正在使用will_paginate成功翻閱記錄。我也通過jQuery使用AJAX實時搜索來更新我的結果div。到目前爲止沒有問題。我的問題是當試圖通過這些實時搜索結果進行分頁時。我只是得到「頁面正在加載...」沒有div更新。我錯過了一些基本的東西?我相信我可能需要以某種方式通過jQuery綁定分頁鏈接的單擊事件。使用will_paginate和AJAX在Rails中使用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
});
});
});
而且,只是好奇,你爲什麼不具備的功能KEYUP延遲?通常,您希望實時搜索等到用戶在運行get之前暫停幾百毫秒,對吧? – 2010-04-09 23:15:59