webcito/bs-select
Composer 安装命令:
composer require webcito/bs-select
包简介
A jQuery-based plugin for creating customizable Bootstrap-style select dropdowns with search, multi-select, and more.
README 文档
README
$.fn.bsSelect
This jQuery plugin enhances standard <select> elements by converting them into fully-featured Bootstrap dropdown
components. It provides a wide range of customization options, methods, and event hooks for seamless integration and
extended functionality. The plugin is built using jQuery 3.6, Bootstrap 5.3, and Bootstrap Icons, ensuring
compatibility and modern design standards.
table of contents
Requirements
- bootstrap 4 or 5
- jQuery
Installation
Download and include the script at the end of the body tag.
<!--suppress ALL --> <script src="dist/locale/de-DE.min.js" type="text/javascript"><!-- optional --> <script src="dist/jquery.bs-select.js" type="text/javascript">
or install with composer and include the script at the end of the body tag.
composer require webcito/bs-select
<!--suppress ALL --> <script src="/vendor/webcito/bs-select/dist/locale/de-DE.min.js" type="text/javascript"> <!-- optional --> <script src="/vendor/webcito/bs-select/dist/jquery.bs-select.min.js" type="text/javascript">
or use the GitHub CDN (jsDelivr):
<script src="https://cdn.jsdelivr.net/gh/ThomasDev-de/bs-select@2.1.38/dist/locale/de-DE.min.js" type="text/javascript"></script> <!-- optional --> <script src="https://cdn.jsdelivr.net/gh/ThomasDev-de/bs-select@2.1.38/dist/jquery.bs-select.min.js" type="text/javascript"></script>
Set global defaults
// multiple options $.bsSelect.setDefaults(options); // get default options $.bsSelect.getDefaults();
Usage
All selects with the attribute [data-bs-toggle="select"] or [data-toggle="select"] are initialized automatically.
<!-- Simple selection --> <!--suppress ALL --> <select name="countries"> <option value="Germany">Deutschland</option> <option value="Poland">Polen</option> ... </select> <!-- Or multiSelection --> <select name="cities" multiple> <option value="1">Berlin rocks</option> <option value="2">New York</option> ... </select> <!-- Or with option groups --> <select name="cities2" multiple> <optgroup label="Germany"> <option value="1">Berlin</option> <option value="2">Munich</option> </optgroup> <optgroup label="USA"> <option value="3">New York</option> <option value="4">San Francisco</option> </optgroup> <optgroup label="Spain"> <option value="5">Barcelona</option> <option value="6">Madrid</option> </optgroup> ... </select> <!-- load jQuery and Bootstrap before --> <script src="dist/jquery.bs-select.js" type="text/javascript"> <script> $('select').bsSelect(); </script>
Locales
Locale files are optional and set the global window.bsSelectTranslations object. Include one locale file before
jquery.bs-select.js.
Available locale files:
cs-CZ, da-DK, de-DE, el-GR, en-GB, en-US, es-ES, fi-FI, fr-FR, hu-HU, it-IT, nb-NO, nl-NL,
pl-PL, pt-PT, ro-RO, sk-SK, sv-SE.
option[data-attributes]
| data-attribute | example | description |
|---|---|---|
| data-subtext | <option data-subtext="Germany" value="1">Berlin</option> |
Adds a small additional text section |
| data-icon | <option data-icon="fa-solid fa-city" value="1">Berlin</option> |
Adds an icon in front of the option. (e.g. a class from Bootstrap Icons) |
Options
| property | data-attribute | type | default | desc |
|---|---|---|---|---|
| value | [data-value] | mixed |
undefined |
If a value is passed here, this value (if available) will be preselected during initialization. Otherwise the value of the native select is adopted.The value is only taken into account during the initial initialization (not for 'updateOptions'). |
| selectAllOnInit | [data-select-all-on-init] | bool |
false |
If true and the select is multiple, all options are selected during initialization. If value is also set, selectAllOnInit takes precedence. |
| nullable | [data-nullable] | bool |
true |
Only relevant for single selects. If false, the select always keeps a selected option and cannot be cleared to empty. |
| search | [data-search] | bool |
true |
adds a search function to the menu. Search matches option text, option data-subtext, and optgroup labels (group label match reveals all options in that group). |
| searchText | [data-search-text] | string |
Search.. |
"Placeholder for search input box* |
| searchQuery | [data-search-query] | string |
null |
predefined search prefix fixed as input-group-text in the search field. The search query becomes prefix + user input. |
| btnWidth | [data-btn-width] | string |
fit-content |
the css width of the dropdown button |
| btnEmptyText | [data-btn-empty-text] | string |
Please select.. |
Text shown when there is no selected value. For single-selects this only applies when nullable is true. |
| btnSplit | [data-btn-split] | string |
false |
create a split button dropdown bootstrap Split button |
| btnClass | [data-btn-class] | string |
btn-outline-secondary |
The classes assigned to the dropdown button |
| dropDirection | [data-drop-direction] | null|string |
null |
opens the DropDown in a desired direction. Possible directions are: dropup|dropend|dropstart|dropdown-center|dropup-center see bootstrap directions |
| dropIconClass | [data-drop-icon-class] | null|string |
bi bi-chevron-down |
If an icon is set here, the dropdown toggle icon is replaced with it. This only works if btnSplit is false. |
| menuHeaderClass | [data-menu-header-class] | string |
text-bg-secondary text-uppercase |
If option groups are present, the background class of the heading is set here. |
| menuInnerClass | [data-menu-inner-class] | null|string |
null |
Set classes for the content of the dropdown menu. |
| menuItemClass | [data-menu-item-class] | string |
null |
The classes are added to the element .dropdown-item. |
| menuMaxHeight | [data-menu-max-height] | number |
300 |
maximum Height of the dropdown list before it starts scrolling. |
| animatedMenu | [data-animated-menu] | bool |
true |
If true, the plugin dropdown menu opens with a small top-to-bottom animation. This uses JavaScript only and does not require additional CSS. |
| menuPreHtml | [data-menu-pre-html] | null|string |
null |
shows a text in the menu before the selection |
| menuAppendHtml | [data-menu-append-html] | null|string |
null |
shows the text in the menu after the selection |
| showSubtext | [data-show-subtext] | bool |
true |
If this option is true, options have the data attribute data-subtext, the subtext will be displayed in the dropdown. |
| showActionMenu | [data-show-action-menu] | bool |
true |
If it is a multiple selection and this option is true, two buttons are displayed above the selection for faster selection. |
| showMultipleCheckboxes | [data-show-action-menu] | bool |
false |
If this option is true, a checkbox is displayed in front of each option instead of the check icon. |
| actionMenuBtnClass | [data-action-menu-btn-class] | string |
btn-light |
The classnames for the buttons in the action menu. |
| showSelectionAsList | [data-show-selection-as-list] | bool |
true |
If it is a multiple selection, all selections should be listed below each other. If the value is false, it will show how much was selected. |
| showSelectedText | function |
(selectedItems, totalItems) => {} |
If it is a multiple selection and the selected elements are greater than 1, this function is called. This function is ignored if the showSelectionAsList option is true. | |
| formatSelectedText | function |
(title, subtext) => {} |
If it is a multiple selection and the selected elements are greater than 1, this function is called. This function is ignored if the showSelectionAsList option is true. | |
| formatItem | function |
($option, title, subtext) => {} |
With this function, all dropdown item can be formated. | |
| deselectAllText | [data-deselect-all-text] | string |
Deselect All |
If showActionMenu is true, the language of the two buttons can be set here. |
| selectAllText | [data-select-all-text] | string |
Select All |
If showActionMenu is true, the language of the two buttons can be set here. |
| checkedIcon | [data-checked-icon] | string |
bi bi-check-lg |
Class used to style the select icon. |
| onBeforeChange | null|function |
($select) => { return true; } |
This function is called before the values are changed. If the function returns true, the change is made, otherwise nothing is changed. | |
| onKeyDown | null|function |
($select, keyEvent) => { // } |
Called when the pressed key is not a dropdown command (arrowUp,arrowDown,ESCAPE) |
Methods
Methods are called as follows
$('select').bsSelect('method', param);
| method | example | description |
|---|---|---|
'show' |
$('select').bsSelect('show'); |
Opens the dropdown menu (does not change overall control visibility) |
'hide' |
$('select').bsSelect('hide'); |
Closes the dropdown menu (does not change overall control visibility) |
'val' |
$('select').bsSelect('val', 1); |
Changes the value of the select |
'search' |
$('select').bsSelect('search', 'query'); |
Programmatically filters the dropdown list. If a searchQuery prefix is set, it will be prepended to the query. |
'selectAll' |
$('select').bsSelect('selectAll'); |
Selects all values |
'selectFirst' |
$('select').bsSelect('selectFirst'); |
Selects the first option element |
'selectLast' |
$('select').bsSelect('selectLast'); |
Selects the last option element |
'selectNone' |
$('select').bsSelect('selectNone'); |
deselects all values |
'clear' |
$('select').bsSelect('clear'); |
Empties the Select |
'updateOptions' |
$('select').bsSelect('updateOptions', {buttonClass: 'btn btn-danger',...}); |
Changes the settings of the dropdown. |
'setBtnClass' |
$('select').bsSelect('setBtnClass', 'btn btn-danger'); |
Behaves like updateOptions. The btnClass option is overwritten. |
'refresh' |
$('select').bsSelect('refresh'); |
Rebuild the dropdown. This is useful if the options are changed via Javascript. |
'getSelectedText' |
$('select').bsSelect('getSelectedText', (text, value) => {}); |
Retrieves the text based on the selections of the select |
'destroy' |
$('select').bsSelect('destroy'[, true]); |
Deletes the dropdown and restores the original select. If parameter is passed true, all data is removed from the element. |
'toggleDisabled' |
$('select').bsSelect('toggleDisabled'); |
Toggles the status of the dropdown button |
'setDisabled' |
$('select').bsSelect('setDisabled', true); |
Set the dropdown disabled (true) otherwise enabled (false) |
'toggleVisibility' |
$('select').bsSelect('toggleVisibility'); |
Toggles the visibility of the dropdown |
'setVisible' |
$('select').bsSelect('setVisible', true); |
Set the visibility of the dropdown (show: true, hide: false) |
Events
| event type | Description |
|---|---|
| init.bs.select | Fires when the plugin has been initialised. |
| hide.bs.select | Fires immediately when the hide instance method has been called. Returns whether value was changed as parameter 1 of the status. |
| hidden.bs.select | Fired when the dropdown has finished being hidden from the user and CSS transitions have completed. |
| show.bs.select | Fires immediately when the show instance method is called. |
| shown.bs.select | Fired when the dropdown has been made visible to the user and CSS transitions have completed. |
| refresh.bs.select | Fires when the refresh method has been invoked. |
| change.bs.select | Fires when the values in the select have changed. Additional parameters are [valueBefore, valueAfter] |
| userChange.bs.select | Fires if the user has changed the value manually |
| clear.bs.select | Fires when the clear method has been called. |
| acceptChange.bs.select | If the function onBeforeChange returns true, this event is fired. |
| cancelChange.bs.select | If the function onBeforeChange returns false, this event is fired. |
| update.bs.select | Fires when the method updateOptions or setBtnClass was called. |
| destroy.bs.select | Fires when the destroy method has been activated. |
| selectAll.bs.select | Fires when the select all option has been pressed. |
| selectNone.bs.select | Fires when the select none option has been pressed. |
| selectFirst.bs.select | Fires when the selectFirst method has been called. The value of the first element is passed as a parameter. |
| selectLast.bs.select | Fires when the selectLast method has been called. The value of the last element is passed as a parameter. |
| any.bs.select | Fires at every event. |
| keydown.bs.select | Fires when the pressed key is not a dropdown command (arrowUp,arrowDown,ESCAPE) |
| toggleDisabled.bs.select | Fires when the disable status changes. Returns the new status as a parameter. fn(e, (boolean)status){} |
Conclusion
The bs-select plugin is a feature-rich solution for enhancing <select> elements to work with Bootstrap styling and
interactivity.
For further queries or issues, check:
Support this project
If this project helps you, feel free to support its development:
webcito/bs-select 适用场景与选型建议
webcito/bs-select 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 330 次下载、GitHub Stars 达 7, 最近一次更新时间为 2023 年 06 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「jquery」 「plugin」 「bootstrap」 「Settings」 「select」 「dropdown」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webcito/bs-select 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webcito/bs-select 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webcito/bs-select 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
Views for the package MedicOneSystems Livewire Datatables with Bootstrap 4
This is a laravel 4 package for the server and client side of datatables at http://datatables.net/
A jQuery augmented PHP library for creating and validating HTML forms
Implementation of carouFredSel as extension for Contao Open Source CMS
统计信息
- 总下载量: 330
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-08