Download the PHP package errohitsinghal/yii2-tom-select without Composer
On this page you can find all versions of the php package errohitsinghal/yii2-tom-select. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download errohitsinghal/yii2-tom-select
More information about errohitsinghal/yii2-tom-select
Files in errohitsinghal/yii2-tom-select
Package yii2-tom-select
Short Description Tom Select widget for Yii2 Framework with Bootstrap 5 support
License MIT
Homepage https://github.com/errohitsinghal/yii2-tom-select
Informations about the package yii2-tom-select
Yii2 Tom Select Widget
A Yii2 widget for Tom Select - a dynamic, framework agnostic, and lightweight (~16kb gzipped) <select> UI control with autocomplete and native-feeling keyboard navigation.
Features
- ✅ Bootstrap 5 integration
- ✅ Local vendor files (no CDN dependencies)
- ✅ Full Tom Select API support
- ✅ All plugins included
- ✅ Model and non-model usage
- ✅ AJAX/Remote data loading
- ✅ Custom rendering templates
- ✅ Event handling
- ✅ Multi-select support
- ✅ Tagging/item creation
- ✅ Diacritics support
Requirements
- PHP >= 7.4
- Yii2 >= 2.0.45
- yii2-bootstrap5 >= 2.0.0
Installation
Install via Composer:
Quick Start
Basic Select
Without Model
Multi-Select
Configuration Options
Widget Properties
| Property | Type | Default | Description |
|---|---|---|---|
type |
string | 'select' |
Input type: 'select' or 'text' |
items |
array | [] |
Option items for select element |
multiple |
bool | false |
Allow multiple selections |
placeholder |
string | null |
Placeholder text |
create |
bool/callable | false |
Allow creating new items |
maxItems |
int/null | 1 |
Maximum items (null for unlimited) |
maxOptions |
int/null | 50 |
Maximum options in dropdown |
plugins |
array | [] |
Tom Select plugins to enable |
clientOptions |
array | [] |
Tom Select configuration options |
clientEvents |
array | [] |
Event handlers |
useBaseAsset |
bool | false |
Use base asset (without plugins) |
hideSelected |
bool | null |
Hide selected items from dropdown |
closeAfterSelect |
bool | null |
Close dropdown after selection |
allowEmptyOption |
bool | false |
Allow empty option |
searchField |
string/array | null |
Fields to search in |
sortField |
string/array | null |
Fields to sort by |
valueField |
string | 'value' |
Property to use as value |
labelField |
string | 'text' |
Property to use as label |
diacritics |
bool | true |
Enable diacritics support |
loadThrottle |
int | 300 |
Load throttle in milliseconds |
preload |
bool/string | false |
Preload options (true, false, or 'focus') |
renderOption |
JsExpression | null |
Custom option render function |
renderItem |
JsExpression | null |
Custom item render function |
load |
JsExpression | null |
Remote data loading function |
variableName |
string | null |
JS variable name for instance |
Available Plugins
Tom Select comes with many built-in plugins:
| Plugin | Description |
|---|---|
caret_position |
Use ← and → arrow keys to move between items |
change_listener |
Update when underlying input changes |
checkbox_options |
Display checkboxes in dropdown |
clear_button |
Add button to clear all selections |
drag_drop |
Allow drag-n-drop sorting of items |
dropdown_header |
Add header to dropdown |
dropdown_input |
Move input to dropdown |
input_autogrow |
Auto-grow input width as user types |
no_active_items |
Disable selecting items |
no_backspace_delete |
Prevent backspace deletion |
optgroup_columns |
Display optgroups as columns |
remove_button |
Add remove button to each item |
restore_on_backspace |
Restore deleted items on backspace |
virtual_scroll |
Virtual scrolling for large lists |
Usage with ActiveForm
The widget integrates seamlessly with Yii2's ActiveForm. You have two options:
Option 1: Using the Widget Directly
Option 2: Using the ActiveField Extension
For a cleaner syntax, use the custom ActiveField class:
ActiveField Helper Methods
| Method | Description |
|---|---|
tomSelect($items, $options) |
Basic Tom Select field |
tomSelectTags($options) |
Pre-configured tagging field with remove buttons |
tomSelectAjax($url, $options) |
Pre-configured AJAX loading field |
Complete Form Example
Form Validation Styling
Tom Select automatically integrates with Bootstrap 5 validation states. When a field has validation errors, the Tom Select control will display with the appropriate error styling.
Usage Examples
With Option Groups
Tagging (Create New Items)
With Plugins and Options
AJAX Remote Data Loading
With Custom Rendering
Event Handling
Virtual Scroll for Large Datasets
Using HTML Options
Dependent/Chained Dropdowns
Pre-populating with Initial Options (AJAX)
API Methods
After initialization, you can access the Tom Select instance through the variable name:
Using Base Asset (Smaller Footprint)
If you don't need all plugins, use the base asset:
Client Options Reference
All Tom Select options are supported via the clientOptions property. Common options include:
| Option | Type | Default | Description |
|---|---|---|---|
options |
array | [] |
Initial options list |
items |
array | [] |
Initial selected values |
create |
bool/function | false |
Allow creating new items |
createOnBlur |
bool | false |
Create item when leaving field |
createFilter |
RegExp/string/function | null |
Filter for new items |
delimiter |
string | ',' |
Delimiter for multiple values |
highlight |
bool | true |
Highlight matches |
persist |
bool | true |
Persist created items |
openOnFocus |
bool | true |
Open dropdown on focus |
maxOptions |
int | 50 |
Max options to display |
maxItems |
int | null |
Max items to select |
hideSelected |
bool | null |
Hide selected from dropdown |
closeAfterSelect |
bool | undefined |
Close after selection |
allowEmptyOption |
bool | false |
Allow empty option |
loadThrottle |
int | 300 |
Throttle for load requests |
loadingClass |
string | 'loading' |
Loading state CSS class |
placeholder |
string | undefined |
Placeholder text |
preload |
bool/string | false |
Preload options |
dropdownParent |
string | null |
Dropdown container |
addPrecedence |
bool | false |
"Add..." is default selection |
selectOnTab |
bool | false |
Select with tab key |
diacritics |
bool | true |
International character support |
duplicates |
bool | false |
Allow duplicate selections |
Events Reference
| Event | Arguments | Description |
|---|---|---|
initialize |
- | Control initialized |
change |
value | Value changed |
focus |
- | Control focused |
blur |
- | Control blurred |
item_add |
value, item | Item added |
item_remove |
value, item | Item removed |
item_select |
item | Item selected |
clear |
- | Selection cleared |
option_add |
value, data | Option added |
option_remove |
value | Option removed |
option_clear |
- | All options cleared |
optgroup_add |
id, data | Option group added |
optgroup_remove |
id | Option group removed |
optgroup_clear |
- | All option groups cleared |
dropdown_open |
dropdown | Dropdown opened |
dropdown_close |
dropdown | Dropdown closed |
type |
str | User typing |
load |
data | Options loaded |
destroy |
- | Control destroyed |
Styling
The widget uses Bootstrap 5 themed CSS by default. You can customize the appearance:
Override CSS Variables
Custom CSS Classes
Troubleshooting
Widget not initializing
Make sure jQuery is loaded if you're using it elsewhere. Tom Select doesn't require jQuery but may conflict if jQuery plugins modify the DOM.
Options not showing
Check that your items array is properly formatted:
AJAX not working
Ensure your endpoint returns JSON in the expected format:
And configure valueField and labelField to match your data structure.
Multiple selection not working
Make sure multiple is set to true:
Support
If you find this package helpful, consider buying me a coffee! ☕
License
This package is open-sourced software licensed under the MIT License.
Credits
- Tom Select - The underlying JavaScript library
- Yii2 Framework - The PHP framework
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Changelog
1.0.0
- Initial release
- Full Tom Select API support
- Bootstrap 5 integration
- All plugins included
- Comprehensive documentation
