我有一个使用ajax获取数据的多个select2。问题是我可以第一次选择一个标签,但是当我第二次尝试时它没有显示ajax数据。 请求工作和数据在那里,我从chrome网络控制台看到。它与第一个请求相同,但select2不显示数据 - 找不到我写的搜索词。 这段代码曾经工作过,在我传递给Rails4之后就停止了工作。我使用select2-rails但实际上也尝试使用最新的select2 js库。结果相同
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
答案 0 :(得分:0)
嗯,事实证明是完全不同的问题。不知何故Mongoid 4通过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