Select2 July 2023
Hi folks
Thanks to @patrickcneuhaus for a success story.
I didn’t really want to go the async check route - surely there’s a better way.
So I downloaded the package from @natestrauser’s Github project,
modified it and put it my app’s packages folder.
I also use Bootstrap 5 in my project, so found a CSS file for Bootstrap 5.
https://apalfrey.github.io/select2-bootstrap-5-theme/
See the tree output of my package below:
├── bootstrap5-theme.css
├── lib
│ └── select2
│ └── dist
│ ├── css
│ │ ├── select2.css
│ │ └── select2.min.css
│ └── js
│ ├── i18n
│ │ ├── af.js
│ │ ├── ar.js
│ │ ├── az.js
│ │ ├── bg.js
│ │ ├── bn.js
│ │ ├── bs.js
│ │ ├── ca.js
│ │ ├── cs.js
│ │ ├── da.js
│ │ ├── de.js
│ │ ├── dsb.js
│ │ ├── el.js
│ │ ├── en.js
│ │ ├── es.js
│ │ ├── et.js
│ │ ├── eu.js
│ │ ├── fa.js
│ │ ├── fi.js
│ │ ├── fr.js
│ │ ├── gl.js
│ │ ├── he.js
│ │ ├── hi.js
│ │ ├── hr.js
│ │ ├── hsb.js
│ │ ├── hu.js
│ │ ├── hy.js
│ │ ├── id.js
│ │ ├── is.js
│ │ ├── it.js
│ │ ├── ja.js
│ │ ├── ka.js
│ │ ├── km.js
│ │ ├── ko.js
│ │ ├── lt.js
│ │ ├── lv.js
│ │ ├── mk.js
│ │ ├── ms.js
│ │ ├── nb.js
│ │ ├── ne.js
│ │ ├── nl.js
│ │ ├── pl.js
│ │ ├── ps.js
│ │ ├── pt-BR.js
│ │ ├── pt.js
│ │ ├── ro.js
│ │ ├── ru.js
│ │ ├── sk.js
│ │ ├── sl.js
│ │ ├── sq.js
│ │ ├── sr-Cyrl.js
│ │ ├── sr.js
│ │ ├── sv.js
│ │ ├── th.js
│ │ ├── tk.js
│ │ ├── tr.js
│ │ ├── uk.js
│ │ ├── vi.js
│ │ ├── zh-CN.js
│ │ └── zh-TW.js
│ ├── select2.full.js
│ ├── select2.full.min.js
│ ├── select2.js
│ └── select2.min.js
├── LICENSE.txt
├── package.js
├── README.md
└── versions.json
Here’s my package.js
Package.describe({
name: "bradzo:select2",
summary: "Select2 is a jQuery based replacement for select boxes.",
version: "4.0.3",
git: "https://github.com/nate-strauser/meteor-select2.git"
});
Package.onUse(function(api) {
// api.versionsFrom("METEOR@0.9.0");
api.versionsFrom("METEOR@1.2");
// api.addFiles([
// "lib/select2/dist/js/select2.js"
// ], "client", { bare: true });
// change addFiles to mainModule
api.mainModule('lib/select2/dist/js/select2.js', 'client');
api.addFiles([
"lib/select2/dist/css/select2.css"
], "client");
api.addFiles([
// https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css
'./bootstrap5-theme.css'
], 'client');
});
I downloaded version 4.0.13 of Select2 from
and dropped the dist folder in lib/ (to follow nate’s original package entry) - I also removed the .gitsubmodules file because I’m not that au fait with submodules 
Set some options for Select2
I wanted to use it inside a Bootstrap modal, and Select2 has me covered
see dropDownParent below.
To make Select2 use the Bootstrap 5 css file, set the theme param to "bootstrap-5’
The data param is set to a local array of JSON objects { id: “abc123”, text: “Option text” }
I also wanted to let the user add entries that aren’t in the select list, hence the tags: true param.
Template.myTemplate.onRendered(function() {
$(document).ready(function() {
$('#selectedCustomer').select2({
// https://select2.org/troubleshooting/common-problems#select2-does-not-function-properly-when-i-use-it-inside-a-bootst
dropdownParent: $('#newInvoiceModal'),
// https://apalfrey.github.io/select2-bootstrap-5-theme/
theme: 'bootstrap-5',
data: items,
// this allows to add a new customer by name
tags: true
});
console.log("doc ready")
});
});
Upshot of all is that I got it to work relatively painlessly, albietly after a couple of days mucking around with other bootstrap select libraries - Select2 seemed to give me the functionality I wanted.
My current Meteor version for my project is 2.7.3
I hope this helps someone else.
Cheers
Brad
PS - I just published this to Atmosphere - bradzo:select2
