我正在将此 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}
我想获得按搜索排序的结果并使用无限滚动功能,但它无法正常工作。
我究竟做错了什么?