Skip to content

Instantly share code, notes, and snippets.

@jdanyow
Created December 18, 2016 21:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jdanyow/8b8f6c593612ec0e64d53501745f7103 to your computer and use it in GitHub Desktop.
Save jdanyow/8b8f6c593612ec0e64d53501745f7103 to your computer and use it in GitHub Desktop.
select2 multiple example
<template>
<require from="select2-custom-attribute"></require>
<h1>Standard Select</h1>
<select multiple value.bind="selectedThing" style="width: 100%">
<option repeat.for="thing of things" model.bind="thing">${thing.name}</option>
</select>
<ul>
<li repeat.for="thing of selectedThing">${thing.name}</li>
</ul>
<h1>Select2</h1>
<div>
<select select2 multiple value.bind="selectedThing2" style="width: 100%">
<option repeat.for="thing of things" model.bind="thing">${thing.name}</option>
</select>
</div>
<ul>
<li repeat.for="thing of selectedThing2">${thing.name}</li>
</ul>
</template>
export class App {
things = [
{ id: 0, name: 'foo' },
{ id: 1, name: 'bar'},
{ id: 2, name: 'baz'}];
selectedThing = [];
selectedThing2 = [];
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script type="text/javascript" src="https://select2.github.io/vendor/js/jquery.min.js"></script>
<script type="text/javascript" src="https://select2.github.io/dist/js/select2.full.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script>
<script>
require(['aurelia-bootstrapper']);
</script>
</body>
</html>
import {customAttribute, inject} from 'aurelia-framework';
@customAttribute('select2')
@inject(Element)
export class Select2CustomAttribute {
constructor(element) {
this.element = element;
}
attached() {
$(this.element).select2(this.value)
.on('change', (e) => {
if (e.originalEvent) {
return;
}
this.element.dispatchEvent(new Event('change'));
});
}
detached() {
$(this.element).select2('destroy');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment