Sha256: 5bcaabefde604aacca5612d77764ba841741c04b3aa20f1b0e50435ad6770c64

Contents?: true

Size: 1.87 KB

Versions: 25

Compression:

Stored size: 1.87 KB

Contents

var selectRemote = function(){

  $('.select2-remote').each(function(){
    if(!$(this).closest('.nested-form-list').length)//Dont execute for nested forms selects
      addSelect2ToField(this);
  });
}

var addSelect2ToField = function(field){
  var fieldParent = $(field).parent();
  var modelName = fieldParent.data('select-model');
  var searchField = fieldParent.data('select-search_field');
  var presenterName = fieldParent.data('select-presenter');

  $(field).select2({
    placeholder: "Selecione ...",
    minimumInputLength: 2,
    width: '100%',
    ajax: buildAjaxFunction(searchField, modelName, presenterName),
    initSelection: function(element, callback) {
      var text = fieldParent.data('select-text');
      var id = fieldParent.data('select-id');
      if(!id)
        return;
      var data = {
        id: id,
        text: text
      } 
      callback(data);
    },
    dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
    escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
  });
  $(field).on("change", function(e) { 
    var select = $(this).parent().find('select');
    select.find('option').remove().end().append('<option value='+e.added.id+'>'+e.added.text+'</option>')
  })

}

var buildAjaxFunction = function(searchField, modelName, presenterName){
  var func = { // instead of writing the function to execute the request we use Select2's convenient helper
        url: "/carnival/load_select_options",
        dataType: 'json',
        quietMillis: 1000,
        data: function (term, page) {
          return {
            q: term,
            search_field: searchField,
            model_name: modelName,
            presenter_name: presenterName
          };
        },
        results: function (data, page) {
          return {results: data};
        }
      }
    return func;
}

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
carnival-0.3.2 app/assets/javascripts/carnival/select_remote.js
carnival-0.3.1 app/assets/javascripts/carnival/select_remote.js
carnival-0.3.0 app/assets/javascripts/carnival/select_remote.js
carnival-0.2.8 app/assets/javascripts/carnival/select_remote.js
carnival-0.2.7 app/assets/javascripts/carnival/select_remote.js
carnival-0.2.6 app/assets/javascripts/carnival/select_remote.js
carnival-0.2.5 app/assets/javascripts/carnival/select_remote.js
carnival-0.2.4 app/assets/javascripts/carnival/select_remote.js
carnival-0.2.3 app/assets/javascripts/carnival/select_remote.js
carnival-0.1.10 app/assets/javascripts/carnival/select_remote.js
carnival-0.2.2 app/assets/javascripts/carnival/select_remote.js
carnival-0.2.1 app/assets/javascripts/carnival/select_remote.js
carnival-0.2.0 app/assets/javascripts/carnival/select_remote.js
carnival-0.1.9 app/assets/javascripts/carnival/select_remote.js
carnival-0.1.8 app/assets/javascripts/carnival/select_remote.js
carnival-0.1.7 app/assets/javascripts/carnival/select_remote.js
carnival-0.1.6 app/assets/javascripts/carnival/select_remote.js
carnival-0.1.5 app/assets/javascripts/carnival/select_remote.js
carnival-0.1.3 app/assets/javascripts/carnival/select_remote.js
carnival-0.1.2 app/assets/javascripts/carnival/select_remote.js