Class kartik\select2\Select2

Inheritancekartik\select2\Select2 » kartik\base\InputWidget » yii\widgets\InputWidget
Uses Traitskartik\base\TranslationTrait, kartik\base\WidgetTrait
Available since version1.0

Select2 widget is a Yii2 wrapper for the Select2 jQuery plugin. This input widget is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results. The widget is specially styled for Bootstrap 3.

See also https://github.com/select2/select2.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$addon array Addon to prepend or append to the Select2 widget - prepend: array|string the prepend addon configuration. kartik\select2\Select2
$changeOnReset bool Whether to trigger change for Select2 input on form reset so the Select2 value is rightly reset. kartik\select2\Select2
$convertFormat boolean Whether the widget should automatically format the date from the PHP DateTime format to the javascript/jquery plugin format. kartik\base\InputWidget
$data array The option data items. kartik\select2\Select2
$disabled boolean Whether input is to be disabled kartik\base\InputWidget
$enablePopStateFix boolean Enable pop state fix for pjax container on press of browser back & forward buttons. kartik\base\InputWidget
$hideSearch bool Whether to hide the search control and render it as a simple select. kartik\select2\Select2
$i18n array The the internalization configuration for this widget. kartik\base\InputWidget
$initValueText string|\kartik\select2\array, The displayed text in the dropdown for the initial value when you do not set or provide data (e.g. using with ajax). kartik\select2\Select2
$language string The locale ID (e.g. 'fr', 'de') for the language to be used by the Select2 Widget. kartik\select2\Select2
$maintainOrder bool Whether to maintain the order of tag / option selected for a multiple select kartik\select2\Select2
$options array The HTML attributes for the input tag. kartik\select2\Select2
$pjaxContainerId string A pjax container identifier if applicable inside which the widget will be rendered. kartik\base\InputWidget
$pluginDestroyJs string The javascript that will be used to destroy the jQuery plugin kartik\base\InputWidget
$pluginEvents array Widget JQuery events. kartik\base\InputWidget
$pluginLoading boolean Show loading indicator while plugin loads kartik\base\InputWidget
$pluginName string The name of the jQuery plugin kartik\select2\Select2
$pluginOptions array Widget plugin options. kartik\base\InputWidget
$readonly boolean Whether input is to be readonly kartik\base\InputWidget
$showToggleAll bool Whether to show the toggle all button for selection all options in a multiple select. kartik\select2\Select2
$size string Size of the Select2 input, must be one of the Select2::LARGE, Select2::MEDIUM or Select2::SMALL. kartik\select2\Select2
$theme string The theme name to be used for styling the Select2. kartik\select2\Select2
$toggleAllSettings array The toggle all button settings for selecting/unselecting all the options. kartik\select2\Select2

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$_dataVar string The HTML5 data variable name that will be used to store the Json encoded pluginOptions within the element on which the jQuery plugin will be initialized. kartik\base\InputWidget
$_encOptions string The JSON encoded plugin options. kartik\base\InputWidget
$_hashVar string The generated hashed variable name that will store the JSON encoded pluginOptions in \kartik\base\View::POS_HEAD. kartik\base\InputWidget
$_inbuiltThemes array List of inbuilt themes kartik\select2\Select2
$_lang string The two or three letter lowercase code for the language according to ISO-639. kartik\base\InputWidget
$_langFile string The language js file. kartik\base\InputWidget
$_loadIndicator string The indicator to be displayed while plugin is loading. kartik\base\InputWidget
$_msgCat string Translation message file category name for i18n. kartik\select2\Select2
$_s2OptionsVar string The variable that will store additional options for Select2 to add enhanced features after the plugin is loaded and initialized. kartik\select2\Select2

Public Methods

Hide inherited methods

MethodDescriptionDefined By
init() kartik\base\InputWidget
initI18N() Yii i18n messages configuration for generating translations kartik\base\TranslationTrait
registerAssetBundle() Registers the asset bundle and locale kartik\select2\Select2
registerAssets() Registers the needed assets kartik\select2\Select2
registerWidgetJs() Registers a JS code block for the widget. kartik\base\WidgetTrait
renderWidget() Initializes and renders the widget kartik\select2\Select2
run() kartik\select2\Select2

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
addAsset() Adds an asset to the view. kartik\base\WidgetTrait
convertDateFormat() Automatically convert the date format from PHP DateTime to Javascript DateTime format kartik\base\InputWidget
embedAddon() Embeds the input group addon kartik\select2\Select2
getInput() Generates an input. kartik\base\InputWidget
getPluginScript() Returns the plugin registration script. kartik\base\WidgetTrait
hashPluginOptions() Generates a hashed variable to store the pluginOptions. The following special data attributes will also be setup for the input widget, that can be accessed through javascript : kartik\base\WidgetTrait
initDestroyJs() Generates the pluginDestroyJs script if it is not set. kartik\base\WidgetTrait
initDisability() Validates and sets disabled or readonly inputs. kartik\base\InputWidget
initInputWidget() Initializes the input widget. kartik\base\InputWidget
initLanguage() Initialize the plugin language. kartik\base\InputWidget
initPlaceholder() Initializes the placeholder for Select2 kartik\select2\Select2
parseBool() Parses the variable for boolean value and returns a right JS expression kartik\select2\Select2
parseDateFormat() Parses and sets plugin date format based on attribute type using \yii\helpers\FormatConverter. Currently this method is used only within the \kartik\date\DatePicker and \kartik\datetime\DateTimePicker\ widgets. kartik\base\InputWidget
registerPlugin() Registers a specific plugin and the related events kartik\base\WidgetTrait
registerPluginOptions() Registers plugin options by storing within a uniquely generated javascript variable. kartik\base\WidgetTrait
renderInput() Renders the source Input for the Select2 plugin. Graceful fallback to a normal HTML select dropdown or text input - in case JQuery is not supported by the browser kartik\select2\Select2
renderToggleAll() Initializes and render the toggle all button kartik\select2\Select2
setDataVar() Sets a HTML5 data variable. kartik\base\WidgetTrait
setLanguage() Sets the language JS file if it exists. kartik\base\InputWidget

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
LARGE 'lg' kartik\select2\Select2
LOAD_PROGRESS '
 
'
kartik\base\InputWidget
MEDIUM 'md' kartik\select2\Select2
SMALL 'sm' kartik\select2\Select2
THEME_BOOTSTRAP 'bootstrap' kartik\select2\Select2
THEME_CLASSIC 'classic' kartik\select2\Select2
THEME_DEFAULT 'default' kartik\select2\Select2
THEME_KRAJEE 'krajee' kartik\select2\Select2

Property Details

$_inbuiltThemes protected static property

List of inbuilt themes

protected static array $_inbuiltThemes = [self::THEME_DEFAULTself::THEME_CLASSICself::THEME_BOOTSTRAPself::THEME_KRAJEE]
$_msgCat protected property

Translation message file category name for i18n.

Translation message file category name for i18n.

protected string $_msgCat 'kvselect'
$_s2OptionsVar protected property

The variable that will store additional options for Select2 to add enhanced features after the

plugin is loaded and initialized. This variable name will be stored as a data attribute `data-s2-options`
within the base select input options.
protected string $_s2OptionsVar null
$addon public property

Addon to prepend or append to the Select2 widget

  • prepend: array|string the prepend addon configuration. If set as a string will be rendered as is. If set as an array, the following properties can be set:
    • content: string, the prepend addon content
    • asButton: bool, whether the addon is a button or button group. Defaults to false.
  • append: array|string the append addon configuration. If set as a string will be rendered as is. If set as an array, the following properties can be set:
    • content: string, the append addon content
    • asButton: bool, whether the addon is a button or button group. Defaults to false.
  • groupOptions: array, HTML options for the input group
  • contentBefore: string, content placed before addon
  • contentAfter: string, content placed after addon
public array $addon = []
$changeOnReset public property

Whether to trigger change for Select2 input on form reset so the Select2 value is rightly reset.

public bool $changeOnReset true
$data public property

The option data items. The array keys are option values, and the array values are the

corresponding option labels. The array can also be nested (i.e. some array values are arrays too). For each
sub-array, an option group will be generated whose label is the key associated with the sub-array. If you
have a list of data models, you may convert them into the format described above using
[[\yii\helpers\ArrayHelper::map()]].
public array $data null
$hideSearch public property

Whether to hide the search control and render it as a simple select. Defaults to false.

public bool $hideSearch false
$initValueText public property

The displayed text in the dropdown for the initial value when you do not set or provide

`data` (e.g. using with ajax). If options['multiple'] is set to `true`, you can set this as an array of text
descriptions for each item in the dropdown `value`.
public string|\kartik\select2\array, $initValueText null
$language public property

The locale ID (e.g. 'fr', 'de') for the language to be used by the Select2 Widget. If this property

not set, then the current application language will be used.
public string $language null
$maintainOrder public property

Whether to maintain the order of tag / option selected for a multiple select

public bool $maintainOrder false
$options public property

The HTML attributes for the input tag. The following options are important:

  • multiple: bool, whether multiple or single item should be selected. Defaults to false.
  • placeholder: string, placeholder for the select item.
public array $options = []
$pluginName public property

The name of the jQuery plugin

public string $pluginName 'select2'
$showToggleAll public property

Whether to show the toggle all button for selection all options in a multiple select.

public bool $showToggleAll true
$size public property

Size of the Select2 input, must be one of the Select2::LARGE, Select2::MEDIUM or Select2::SMALL. Defaults to Select2::MEDIUM.

public string $size self::MEDIUM
$theme public property

The theme name to be used for styling the Select2.

public string $theme self::THEME_KRAJEE
$toggleAllSettings public property

The toggle all button settings for selecting/unselecting all the options. This is applicable only for multiple select. The following array key properties can be set:

  • selectLabel: string, the markup to be shown to select all records. Defaults to <i class="glyphicon glyphicon-unchecked"></i> Select all.
  • unselectLabel: string, the markup to be shown to unselect all records. Defaults to <i class="glyphicon glyphicon-checked"></i> Unselect all.
  • selectOptions: array, the HTML attributes for the container wrapping the select label. Defaults to [].
  • unselectOptions: array, the HTML attributes for the container wrapping the unselect label. Defaults to [].
  • options: array, the HTML attributes for the toggle button container. Defaults to: ['class' => 's2-togall-button'].

Method Details

embedAddon() protected method

Embeds the input group addon

protected string embedAddon ( $input )
$input string
initPlaceholder() protected method

Initializes the placeholder for Select2

protected void initPlaceholder ( )
parseBool() protected static method

Parses the variable for boolean value and returns a right JS expression

protected static \yii\web\JsExpression parseBool ( $var )
$var mixed

The variable value to parse

registerAssetBundle() public method

Registers the asset bundle and locale

public void registerAssetBundle ( )
registerAssets() public method

Registers the needed assets

public void registerAssets ( )
renderInput() protected method

Renders the source Input for the Select2 plugin. Graceful fallback to a normal HTML select dropdown or text input - in case JQuery is not supported by the browser

protected void renderInput ( )
renderToggleAll() protected method

Initializes and render the toggle all button

protected void renderToggleAll ( )
renderWidget() public method

Initializes and renders the widget

public void renderWidget ( )
run() public method

public void run ( )