Caner
Caner

Reputation: 1498

Select2 multiple ajax data doesn't load on second selection

I have a multiple select2 that uses ajax to get data. The problem is that I can select a tag for the first time but when I try for the second time it doesn't show up ajax data. Request works and data is there and I see from chrome network console. It's the same as the first request but select2 doesn't show data- can't find the search term I write. This code used to work, it stopped working after I passed to Rails4. I use select2-rails but actually tried it with the latest select2 js library too. The same results

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() 

Controller

  def list_styles  
     if params[:q].present?
        @categories = Category.search(params[:q], page: params[:page])
        respond_with @categories 
     end
  end

Upvotes: 0

Views: 1254

Answers (1)

Caner
Caner

Reputation: 1498

Well It turned out to completely different issue. Somehow Mongoid 4 through searchkick sent the data in $oid format. I created a mongoid initialiser and placed this code and things started to work...

module BSON
  class ObjectId
    def to_json(*args)
      to_s.to_json
    end

    def as_json(*args)
      to_s.as_json
    end
  end
end

Upvotes: 0

Related Questions