bootstrap-select下拉框详解Core options(来自官方文档)

罗兴运
2023-12-01

Core options


Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-style="" or data-selected-text-format="count".

Note: For security reasons, the sanitizesanitizeFn and whiteList options cannot be supplied using data attributes.

NameTypeDefaultDescription
actionsBoxbooleanfalse

When set to true, adds two buttons to the top of the dropdown menu (Select All & Deselect All).

containerstring | falsefalse

When set to a string, appends the select to a specific element or selector, e.g., container: 'body' | '.main-body'

countSelectedTextstring | functionfunction

Sets the format for the text displayed when selectedTextFormat is count or count > #. {0} is the selected amount. {1} is total available for selection.

When set to a function, the first parameter is the number of selected options, and the second is the total number of options. The function must return a string.

deselectAllTextstring'Deselect All'

The text on the button that deselects all options when actionsBox is enabled.

dropdownAlignRightboolean | 'auto'false

Align the menu to the right instead of the left. If set to 'auto', the menu will automatically align right if there isn't room for the menu's full width when aligned to the left.

dropupAutobooleantrue

checks to see which has more room, above or below. If the dropup has enough room to fully open normally, but there is more room above, the dropup still opens normally. Otherwise, it becomes a dropup. If dropupAuto is set to false, dropups must be called manually.

headerstringfalse

adds a header to the top of the menu; includes a close button by default

hideDisabledbooleanfalse

removes disabled options and optgroups from the menu data-hide-disabled: true

iconBasestring'glyphicon'

Set the base to use a different icon font instead of Glyphicons. If changing iconBase, you might also want to change tickIcon, in case the new icon font uses a different naming scheme.

liveSearchbooleanfalse

When set to true, adds a search box to the top of the selectpicker dropdown.

liveSearchNormalizebooleanfalse

Setting liveSearchNormalize to true allows for accent-insensitive searching.

liveSearchPlaceholderstringnull

When set to a string, a placeholder attribute equal to the string will be added to the liveSearch input.

liveSearchStylestring'contains'

When set to 'contains', searching will reveal options that contain the searched text. For example, searching for pl with return both Apple, Plum, and Plantain. When set to 'startsWith', searching for pl will return only Plum and Plantain.

maxOptionsinteger | falsefalse

When set to an integer and in a multi-select, the number of selected options cannot exceed the given value.

This option can also exist as a data-attribute for an <optgroup>, in which case it only applies to that <optgroup>.

maxOptionsTextstring | array | functionfunction

The text that is displayed when maxOptions is enabled and the maximum number of options for the given scenario have been selected.

If a function is used, it must return an array. array[0] is the text used when maxOptions is applied to the entire select element. array[1] is the text used when maxOptions is used on an optgroup. If a string is used, the same text is used for both the element and the optgroup.

mobilebooleanfalse

When set to true, enables the device's native menu for select menus.

multipleSeparatorstring', '

Set the character displayed in the button that separates selected options.

noneSelectedTextstring'Nothing selected'

The text that is displayed when a multiple select has no selected options.

noneResultsTextstring'No results matched {0}'

The text displayed when a search doesn't return any results.

selectAllTextstring'Select All'

The text on the button that selects all options when actionsBox is enabled.

selectedTextFormat'values' | 'static' | 'count' | 'count > x'(where x is an integer)'values'

Specifies how the selection is displayed with a multiple select.

'values' displays a list of the selected options (separated by multipleSeparator'static' simply displays the select element's title. 'count' displays the total number of selected options. 'count > x'behaves like 'values' until the number of selected options is greater than x; after that, it behaves like 'count'.

selectOnTabbooleanfalse

When set to true, treats the tab character like the enter or space characters within the selectpicker dropdown.

showContentbooleantrue

When set to true, display custom HTML associated with selected option(s) in the button. When set to false, the option value will be displayed instead.

showIconbooleantrue

When set to true, display icon(s) associated with selected option(s) in the button.

showSubtextbooleanfalse

When set to true, display subtext associated with a selected option in the button.

showTickbooleanfalse

Show checkmark on selected option (for items without multiple attribute).

size'auto' | integer | false'auto'

When set to 'auto', the menu always opens up to show as many items as the window will allow without being cut off.

When set to an integer, the menu will show the given number of items, even if the dropdown is cut off.

When set to false, the menu will always show all items.

stylestring | nullBootstrap 4: 'btn-light' Bootstrap 3: 'btn-default'

When set to a string, add the value to the button's style.

styleBasestring | null'btn'

The default class applied to the button. When using the setStyle method, this class will always remain.

Hint: If you want to style the element as a form control, set style to '' and styleBase to 'form-control'.

tickIconstring'glyphicon-ok'

Set which icon to use to display as the "tick" next to selected options.

titlestring | nullnull

The default title for the selectpicker.

virtualScrollboolean | integer600

If enabled, the items in the dropdown will be rendered using virtualization (i.e. only the items that are within the viewport will be rendered). This drastically improves performance for selects with a large number of options. Set to an integer to only use virtualization if the select has at least that number of options.

width'auto' | 'fit' | css-width | false (where css-widthis a CSS width with units, e.g. 100px)false

When set to auto, the width of the selectpicker is automatically adjusted to accommodate the widest option.

When set to a css-width, the width of the selectpicker is forced inline to the given value.

When set to false, all width information is removed.

windowPaddinginteger | array0

This is useful in cases where the window has areas that the dropdown menu should not cover - for instance a fixed header. When set to an integer, the same padding will be added to all sides. Alternatively, an array of integers can be used in the format [top, right, bottom, left].

sanitizebooleantrue

Enable or disable the sanitization. If activated, 'data-content' on individual options will be sanitized.

whiteListobjectDefault value

Object which contains allowed attributes and tags

sanitizeFnnull | functionnull

Here you can supply your own sanitize function. This can be useful if you prefer to use a dedicated library to perform sanitization.

Default settings


You can change the default settings for bootstrap-select by modifying its Constructor.DEFAULTS object:

$.fn.selectpicker.Constructor.DEFAULTS.multipleSeparator = ' | ';

Events


Bootstrap-select exposes a few events for hooking into select functionality.

hide.bs.select, hidden.bs.select, show.bs.select, and shown.bs.select all have a relatedTarget property, whose value is the toggling anchor element.

Event TypeDescription
show.bs.selectThis event fires immediately when the show instance method is called.
shown.bs.selectThis event is fired when the dropdown has been made visible to the user (will wait for CSS transitions, to complete).
hide.bs.selectThis event is fired immediately when the hide instance method has been called.
hidden.bs.selectThis event is fired when the dropdown has finished being hidden from the user (will wait for CSS transitions, to complete).
loaded.bs.selectThis event fires after the select has been initialized.
rendered.bs.selectThis event fires after the render instance has been called.
refreshed.bs.selectThis event fires after the refresh instance has been called.
changed.bs.select

This event fires after the select's value has been changed. It passes through eventclickedIndexisSelectedpreviousValue.

previousValue is the value of the select prior to being changed. If the select's value has been changed either via the .selectpicker('val').selectpicker('selectAll'), or .selectpicker('deselectAll') methods, clickedIndex and isSelected will be null.

$('#mySelect').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
  // do something...
});

Sanitizer


HTML added via the data-content attribute on individual options is sanitized using our built-in sanitizer.

The default whiteList value is the following:

var ARIA_ATTRIBUTE_PATTERN = /^aria-[\w-]*$/i;
var DefaultWhitelist = {
  // Global attributes allowed on any supplied element below.
  '*': ['class', 'dir', 'id', 'lang', 'role', 'tabindex', 'style', ARIA_ATTRIBUTE_PATTERN],
  a: ['target', 'href', 'title', 'rel'],
  area: [],
  b: [],
  br: [],
  col: [],
  code: [],
  div: [],
  em: [],
  hr: [],
  h1: [],
  h2: [],
  h3: [],
  h4: [],
  h5: [],
  h6: [],
  i: [],
  img: ['src', 'alt', 'title', 'width', 'height'],
  li: [],
  ol: [],
  p: [],
  pre: [],
  s: [],
  small: [],
  span: [],
  sub: [],
  sup: [],
  strong: [],
  u: [],
  ul: []
}

If you want to add new values to this default whiteList you can do the following:

var myDefaultWhiteList = $.fn.selectpicker.Constructor.DEFAULTS.whiteList;

// To allow table elements
myDefaultWhiteList.table = [];

// To allow td elements and data-option attributes on td elements
myDefaultWhiteList.td = ['data-option'];

// You can push your custom regex to validate your attributes.
// Be careful about your regular expressions being too lax
var myCustomRegex = /^data-my-app-[\w-]+/;
myDefaultWhiteList['*'].push(myCustomRegex);

If you want to bypass our sanitizer because you prefer to use a dedicated library, you should do the following:

$('#yourSelect').selectpicker({
  sanitizeFn: function (domNodes) {
    return DOMPurify.sanitize(domNodes)
  }
});

For performance reasons, our built-in sanitizer accepts an array of DOM nodes as its first argument, rather than an HTML string. Keep that in mind if deciding to use your own sanitizeFn.

 类似资料: