How to use select2 - 10 common examples

To help you get started, we’ve selected a few select2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github select2 / select2 / tests / selection / search-tests.js View on Github external
test('the focus event shifts the focus', function (assert) {
  // Check for IE 8, which triggers a false negative during testing
  if (window.attachEvent && !window.addEventListener) {
    // We must expect 0 assertions or the test will fail
    assert.expect(0);
    return;
  }

  var $container = $('#qunit-fixture .event-container');
  var container = new MockContainer();

  var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);

  var $element = $('#qunit-fixture .multiple');
  var selection = new CustomSelection($element, options);

  var $selection = selection.render();
  selection.bind(container, $container);

  // Update the selection so the search is rendered
  selection.update([]);

  // Make it visible so the browser can place focus on the search
  $container.append($selection);

  // The search should not be automatically focused

  var $search = $selection.find('input');
github select2 / select2 / tests / results / infiniteScroll-tests.js View on Github external
assert.expect(1);

  var $ = require('jquery');

  var $select = $('<select></select>');

  var $container = $('<span></span>');
  var container = new MockContainer();

  var Utils = require('select2/utils');
  var Options = require('select2/options');

  var Results = require('select2/results');
  var InfiniteScroll = require('select2/dropdown/infiniteScroll');

  var InfiniteScrollResults = Utils.Decorate(Results, InfiniteScroll);

  var results = new InfiniteScrollResults($select, new Options({}));

  // Fake the data adapter for the `setClasses` method
  results.data = {};
  results.data.current = function (callback) {
    callback([{ id: 'test' }]);
  };

  $('#qunit-fixture').append(results.render());

  results.bind(container, $container);

  results.on('query:append', function () {
    assert.ok(true, 'It tried to load more immediately');
  });
github select2 / select2 / tests / integration / jquery-calls.js View on Github external
$select.select2({maximumSelectionLength: 2, multiple: true});
  
  assert.equal(
    $select.val(),
    '3',
    'Only 1 option should be pulled.'
  );

  // Try to resolve instance via .data('select2').
  var $instance = $select.data('select2'); 
  assert.ok($instance);
  assert.ok($instance.options);

  // Ensure $select.data('select2') is the same instance 
  // created by .select2()   
  assert.equal($instance, Utils.GetData($instance.$element[0], 
               'select2'));
   
  // Ensure initialized property matches.
  assert.equal($instance.options.options.maximumSelectionLength,
               2);
});
github select2 / select2 / tests / selection / search-tests.js View on Github external
selection.update([
    {
      id: '1',
      text: 'One'
    }
  ]);

  var $search = $selection.find('input');
  var $choices = $selection.find('.select2-selection__choice');

  assert.equal($search.length, 1, 'The search was visible');
  assert.equal($choices.length, 1, 'The choice was rendered');

  // Trigger the backspace on the search
  var backspace = $.Event('keydown', {
    which: KEYS.BACKSPACE
  });
  $search.trigger(backspace);
});
github select2 / select2 / tests / helpers.js View on Github external
// Restore the require/define
var require = $.fn.select2.amd.require;
var define = $.fn.select2.amd.define;

// Disable jQuery's binding to $
jQuery.noConflict();

var Utils = require('select2/utils');

function MockContainer () {
  MockContainer.__super__.constructor.call(this);
}

Utils.Extend(MockContainer, Utils.Observable);

MockContainer.prototype.isOpen = function () {
  return this.isOpen;
};

var log = [];
var testName;

QUnit.done(function (test_results) {
  var tests = [];
  for(var i = 0, len = log.length; i &lt; len; i++) {
    var details = log[i];
    tests.push({
      name: details.name,
      result: details.result,
      expected: details.expected,
github select2 / select2 / tests / selection / allowClear-tests.js View on Github external
test('clear button doesnt visually break selected options', function (assert) {
  var $element = $('<select></select>');

  var Selection = Utils.Decorate(
    Utils.Decorate(MultipleSelection, Placeholder),
    AllowClear
  );

  var selection = new Selection(
    $element,
    allowClearOptions
  );
  var container = new MockContainer();

  var $container = $(
    '<span class="select2-container select2-container--default"></span>'
  );
  $('#qunit-fixture').append($container);

  var $selection = selection.render();
github select2 / select2 / tests / utils / decorator-tests.js View on Github external
return decorated.call(this, a + 'b');
  };

  function DecoratorClass (decorated, testArgument) {
    this.decoratorCalled = true;
    this.decoratorTestArgument = testArgument;

    decorated.call(this, testArgument);
  }

  DecoratorClass.prototype.test = function (decorated, a) {
    return decorated.call(this, a + 'a');
  };

  var DecoratedClass = Utils.Decorate(
    Utils.Decorate(BaseClass, MiddleClass),
    DecoratorClass
  );

  var inst = new DecoratedClass('test');

  assert.ok(inst.baseCalled, 'The base class contructor was called');
  assert.ok(inst.middleCalled, 'The middle class constructor was called');
  assert.ok(inst.decoratorCalled, 'The decorator constructor was called');

  assert.strictEqual(inst.baseTestArgument, 'test');
  assert.strictEqual(inst.middleTestArgument, 'test');
  assert.strictEqual(inst.decoratorTestArgument, 'test');

  var out = inst.test('test');

  assert.strictEqual(out, 'testabc');
github select2 / select2 / tests / dropdown / search-a11y-tests.js View on Github external
module('Dropdown - Search - Accessibility');

var Utils = require('select2/utils');

var Dropdown = require('select2/dropdown');
var DropdownSearch = Utils.Decorate(
  Dropdown,
  require('select2/dropdown/search')
);

var $ = require('jquery');

var Options = require('select2/options');
var options = new Options({});

test('role attribute is set to searchbox', function (assert) {
  var $select = $('#qunit-fixture .single');

  var dropdown = new DropdownSearch($select, options);
  var $dropdown = dropdown.render();

  var container = new MockContainer();
github select2 / select2 / tests / utils / decorator-tests.js View on Github external
MiddleClass.prototype.test = function (decorated, a) {
    return decorated.call(this, a + 'b');
  };

  function DecoratorClass (decorated, testArgument) {
    this.decoratorCalled = true;
    this.decoratorTestArgument = testArgument;

    decorated.call(this, testArgument);
  }

  DecoratorClass.prototype.test = function (decorated, a) {
    return decorated.call(this, a + 'a');
  };

  var DecoratedClass = Utils.Decorate(
    Utils.Decorate(BaseClass, MiddleClass),
    DecoratorClass
  );

  var inst = new DecoratedClass('test');

  assert.ok(inst.baseCalled, 'The base class contructor was called');
  assert.ok(inst.middleCalled, 'The middle class constructor was called');
  assert.ok(inst.decoratorCalled, 'The decorator constructor was called');

  assert.strictEqual(inst.baseTestArgument, 'test');
  assert.strictEqual(inst.middleTestArgument, 'test');
  assert.strictEqual(inst.decoratorTestArgument, 'test');

  var out = inst.test('test');
github select2 / select2 / tests / selection / allowClear-tests.js View on Github external
module('Selection containers - Placeholders - Allow clear');

var Placeholder = require('select2/selection/placeholder');
var AllowClear = require('select2/selection/allowClear');

var SingleSelection = require('select2/selection/single');
var MultipleSelection = require('select2/selection/multiple');

var $ = require('jquery');
var Options = require('select2/options');
var Utils = require('select2/utils');

var AllowClearPlaceholder = Utils.Decorate(
  Utils.Decorate(SingleSelection, Placeholder),
  AllowClear
);

var allowClearOptions = new Options({
  placeholder: {
    id: 'placeholder',
    text: 'This is the placeholder'
  },
  allowClear: true
});

test('clear is not displayed for single placeholder', function (assert) {
  var selection = new AllowClearPlaceholder(
    $('#qunit-fixture .single-with-placeholder'),
    allowClearOptions
  );

select2

Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results.

MIT
Latest version published 4 years ago

Package Health Score

72 / 100
Full package analysis