[英]select2 plugin and json format rails 3
我正在將此gem用於自動完成功能:
https://github.com/argerim/select2-rails和select2。
這是我的動作:
def autocomplete_subject
@messages = Message.where(:by_the_system => nil).page(params[:page]).per(params[:page_limit])
messages = Array.new
@messages.each do |m|
element = Hash.new
element[:id] = m.id
element[:text] = [m.subject].join(' ')
messages << element
end
results = Hash.new
results[:results] = messages
respond_to do |format|
format.json {
render :json => {
:results => results,
:total => @messages.count,
}
}
end
end
這是我的javascript:
function messageFormatSelection(message) {
return message.subject;
}
$("#search_boxes #subject").select2({
minimumInputLength: 3,
multiple: true,
width: "300px;",
ajax: {
url: "/admin/messages/autocomplete_subject_nil.json",
dataType: 'json',
quietMillis: 100,
data: function (term, page) { // page is the one-based page number tracked by Select2
return {
subject: term, //search term
page_limit: 5, // page size
page: page, // page number
};
},
results: function (data, page) {
var more = (page * 5) < data.total;
return {results: data.results, more: more};
}
},
formatSelection: messageFormatSelection // omitted for brevity, see the source of this page
});
這是我的json視圖:
{"results":{"results":[{"id":"50b4f5c01d41c811fb000014","text":"Microfunc con id: 50ae41011d41c86ad8000004"},{"id":"50b4f8d31d41c811fb000026","text":"Re: Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4fcf61d41c811fb00002e","text":"Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4fe531d41c811fb000032","text":"Re: Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4ff431d41c811fb000038","text":"Re: Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4f5d71d41c811fb000017","text":"Re: Microfunc con id: 50ae41011d41c86ad8000004"},{"id":"50b4f8231d41c811fb000024","text":"Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4fa921d41c811fb000028","text":"Re: Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4fee41d41c811fb000034","text":"Re: Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4ff291d41c811fb000036","text":"Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4ffd71d41c811fb00003a","text":"mira os poneis deacuerdo o que"},{"id":"50b5013d1d41c811fb00004b","text":"Re: Microfunc con id: 50ae41011d41c86ad8000004"},{"id":"50b51ccb1d41c811fb000061","text":"mensaje enviado desde el panel de administraci\u00f3n"},{"id":"50b51e0c1d41c811fb000063","text":"Re: mensaje enviado desde el panel de administraci\u00f3n"},{"id":"50b51e461d41c811fb000065","text":"Microfunc con id: 50ae3b321d41c849c500000b"},{"id":"50b51e611d41c811fb000068","text":"Re: Microfunc con id: 50ae3b321d41c849c500000b"},{"id":"50b61b831d41c80b5d000004","text":"Re: mira os poneis deacuerdo o que"},{"id":"50b897a41d41c83613000034","text":"Microfunc con id: 50ae3b321d41c849c500000b"}]},"total":18}
我想按搜索結果排序並使用無限滾動功能,但是它無法正常工作。
我究竟做錯了什么?
排序需要在服務器端而不是在客戶端完成,因為客戶端在加載時無法訪問所有元素。 從您的代碼看來, text
等同於您的Message subject
。 如果是這樣,您可以簡單地修改以下行:
@messages = Message.where(:by_the_system => nil).
page(params[:page]).per(params[:page_limit])
包括排序順序:
@messages = Message.where(:by_the_system => nil).
order(:subject).
page(params[:page]).per(params[:page_limit])
您的回答應包括當前頁面,以及這是否是最后一頁:
respond_to do |format|
format.json {
render :json => {
:results => results,
:total => @messages.count,
:page => params[:page].to_i,
:last_page => @messages.last_page
}
}
您還需要修改ajax請求以更新頁面變量,而不是未使用的“更多”偏移量:
results: function (data, page) {
page = data.page + 1;
if (data.last_page == data.page){
// do something to disable searching as list is now exhausted
}
return {results: data.results};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.