Sha256: 0d8638c0f43c4be9d3be57b54aabef041fd09458069562b19229429821a712e3

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 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
    };
    $.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 );
      }
    });
    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.0 app/assets/javascripts/activeadmin/selectize_input.js