Sha256: 1b24284b0391aecdee755c48d7d5103b4df4263d5a99ca3af3e08d65f04b3c75

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

$(document).ready( function() {
  $('.selectized').each( function() {
    var remote = $(this).attr( 'data-opt-remote' ) ? $(this).attr( 'data-opt-remote' ) : '';
    var field_text = $(this).attr( 'data-opt-text' ) ? $(this).attr( 'data-opt-text' ) : 'name';
    var field_value = $(this).attr( 'data-opt-value' ) ? $(this).attr( 'data-opt-value' ) : 'id';
    var opts = {
      closeAfterSelect: true,
      create: false,
      hideSelected: true,
      labelField: field_text,
      options: [],
      plugins: ['remove_button'],
      searchField: field_text,
      valueField: field_value
    };

    if (!$(this).data('options') ) {
      $.each( this.attributes, function( i, attr ) {
        if( attr.name.startsWith( 'data-opt-' ) ) {
          var name = attr.name.substr( 9 );
          if( name != 'remote' && name != 'text' && name != 'value' ) opts[name] = ( attr.value == 'true' ) ? true : ( ( attr.value == 'false' ) ? false : attr.value );
        }
      });
    }
    else {
      opts = $.extend({}, opts, $(this).data('options'));
    }

    opts['load'] = function( query, callback ) {
      if( !query.length ) return callback();
      $.ajax({
        url: remote + '?q[' + field_text + '_contains]=' + encodeURIComponent( query ),
        type: 'GET',
        error: function() {
          callback();
        },
        success: function(res) {
          callback( res.slice( 0, 10 ) );
        }
      });
    };
    $(this).selectize( opts );
  });
});

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activeadmin_selectize-0.1.2 app/assets/javascripts/activeadmin/selectize_input.js