我有一个multiple select2,它使用ajax获取数据。问题是,我可以第一次选择一个标签,但当我第二次尝试时,它不会显示ajax数据。请求有效,数据在那里,我从chrome网络控制台看到。这与第一个请求相同,但select2没有显示数据-找不到我写的搜索词。这个代码曾经有效,但在我转到Rails4后就停止了。我使用select2 rails,但实际上也在最新的select2js库中尝试过。的结果相同
Coffescript
if $("#categories").val().length is 0
$("#categories").val "initialValue"
$("#categories").select2
closeOnSelect:false
containerCssClass: "select2-default"
formatNoMatches: ->
I18n.t "shared.navbar.no_matches_found"
formatInputTooShort: (input, min) ->
I18n.t("shared.navbar.please_select") + " " + (min - input.length) + " " + I18n.t("shared.navbar.more_characters")
formatSelectionTooBig: (limit) ->
I18n.t("shared.navbar.you_can_only_select") + " " + limit + " " + I18n.t("shared.navbar.item") + ((if limit is 1 then "" else "s"))
formatLoadMore: (pageNumber) ->
I18n.t "shared.navbar.loading_more_results"
formatSearching: ->
I18n.t "shared.navbar.searching"
minimumInputLength: 3
width: "100%"
multiple: true
ajax:
url: "/categories/list_styles"
dataType: "json"
quietMillis: 100
data: (term, page) ->
q: term
page_limit: 10
page: page
results: (data) ->
hashtable = {}
results = []
$.each data, (index, item) ->
if hashtable[item.parent] is `undefined`
hashtable[item.parent] =
text: item.parent
children: []
results.push hashtable[item.parent]
hashtable[item.parent].children.push
id: item._id
text: item.title
results: results
initSelection: (element, callback) ->
data1 = []
if element.val() is "initialValue"
element.val('')
jQuery(element.val().split(",")).each ->
$.ajax
type: "get"
url: "/providers/list_categories"
async: false
dataType: "json"
data: { id: $("#provider_id").val(), selected: $("#categories").val().split(",") }
success: (category) ->
$.each category, (i, obj) ->
data1.push
id: @_id
text: @title
callback data1
$.each $(".select2-container"), (i, n) ->
$(n).next().show().fadeTo(0, 0).height("0px").css "left", "auto" # make the original select visible for validation engine and hidden for us
$(n).prepend $(n).next()
$(n).delay(500).queue ->
#$(this).removeClass "validate[required]" #remove the class name from select2 container(div), so that validation engine dose not validate it
$(this).dequeue()
控制器
def list_styles
if params[:q].present?
@categories = Category.search(params[:q], page: params[:page])
respond_with @categories
end
end
结果是完全不同的问题。不知怎的,Mongoid4通过searchkick发送了$oid格式的数据。我创建了一个mongoid初始化程序,并放置了这些代码,事情就开始工作了。。。
module BSON
class ObjectId
def to_json(*args)
to_s.to_json
end
def as_json(*args)
to_s.as_json
end
end
end