Sha256: 6a382df8c28552a16cc0966115b669efed8c7a4deefa9a13dbf7cf0b092cb8f1

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

$ ->
  $('.filter_ajax_select select').each (_, select) ->
    select = $(select)
    valueField = select.data('value-field')
    searchFields = select.data('search-fields').split(' ')
    ordering = select.data('ordering')

    loadOptions = (q, callback) ->
      $.ajax
        url: 'users/filter'
        type: 'GET'
        dataType: 'json'
        data:
          q: q
          limit: select.data('limit')
          order: ordering
        error: ->
          callback()
        success: (res) ->
          callback(res)

    select.selectize
      valueField: valueField
      labelField: searchFields[0]
      searchField: searchFields
      sortField: ordering.split(',').map (clause)->
        c = clause.trim().split(' ')
        { field: c[0], direction: c[1] }
      options: []
      create: false
      render:
        option: (item, escape) ->
          html = searchFields.map (field, index)->
            value = escape(item[field])

            if index == 0
              klass = 'primary'
            else
              klass = 'secondary'

            "<span class='#{klass}'>#{value}</span>"

          "<div class='item'>#{html.join('')}</div>"

      load: (query, callback) ->
        if query.length
          q = {}
          q[select.data('ransack')] = query
          loadOptions(q, callback)
        else
          callback()

      onInitialize: ->
        selectize = this
        selectedValue = select.data('selected-value')
        selectedRansack = "#{valueField}_eq"

        if selectedValue
          q = {}
          q[selectedRansack] = selectedValue

          loadOptions(q, (res)->
            if res.length
              selectize.addOption(res[0])
              selectize.addItem(res[0][valueField])
          )

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activeadmin-ajax_filter-0.2.1 app/assets/javascripts/activeadmin-ajax_filter.js.coffee
activeadmin-ajax_filter-0.2.0 app/assets/javascripts/activeadmin-ajax_filter.js.coffee