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 the prepend addon configuration - content: string the prepend addon content - asButton: boolean whether the addon is a button or button group. 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
$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
$options array The HTML attributes for the input tag. kartik\select2\Select2
$pluginEvents array Widget JQuery events. kartik\base\InputWidget
$pluginLoading mixed Show loading indicator while plugin loads kartik\base\InputWidget
$pluginName string The name of the jQuery plugin kartik\base\InputWidget
$pluginOptions array Widget plugin options kartik\base\InputWidget
$readonly boolean Whether input is to be readonly kartik\base\InputWidget
$size string Size of the Select2 input, must be one of the LARGE, MEDIUM or SMALL. kartik\select2\Select2
$theme string The theme name to be used for styling the Select2 kartik\select2\Select2

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$_dataVar string The hashed variable to store the pluginOptions kartik\base\InputWidget
$_encOptions string The Json encoded options kartik\base\InputWidget
$_hashVar string The hashed variable to store the pluginOptions kartik\base\InputWidget
$_inbuiltThemes 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 for loading kartik\base\InputWidget
$_msgCat string Translation message file category name for i18n kartik\base\InputWidget

Public Methods

Hide inherited methods

MethodDescriptionDefined By
init() kartik\select2\Select2
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

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 : - 'data-krajee-{name}' will store the hashed variable storing the plugin options. The {name} tag will represent the plugin name (e.g. select2, typeahead etc.) - Fixes issue #6. kartik\base\WidgetTrait
initDisability() Validates and sets disabled or readonly inputs kartik\base\InputWidget
initLanguage() Initialize the plugin language kartik\base\InputWidget
initPlaceholder() Initializes the placeholder for Select2 kartik\select2\Select2
parseDateFormat() Parses date format based on attribute type using yii\helpers\FormatConverter Used only within DatePicker and DateTimePicker. kartik\base\InputWidget
registerPlugin() Registers a specific plugin and the related events kartik\base\WidgetTrait
registerPluginOptions() Registers plugin options by storing it in a hashed javascript variable kartik\base\WidgetTrait
renderInput() Renders the source Input for the Select2 plugin. kartik\select2\Select2
setDataVar() Sets 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
protected static $_inbuiltThemes = [self::THEME_DEFAULTself::THEME_CLASSICself::THEME_BOOTSTRAPself::THEME_KRAJEE]
$addon public property

Addon to prepend or append to the Select2 widget

  • prepend: array the prepend addon configuration
    • content: string the prepend addon content
    • asButton: boolean whether the addon is a button or button group. Defaults to false.
  • append: array the append addon configuration
    • content: string the append addon content
    • asButton: boolean 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
$options public property

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

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

Size of the Select2 input, must be one of the LARGE, MEDIUM or SMALL. Defaults to 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

Method Details

embedAddon() protected method

Embeds the input group addon

protected string embedAddon ( $input )
$input string
init() public method

public void init ( )
initPlaceholder() protected method

Initializes the placeholder for Select2

protected void initPlaceholder ( )
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 ( )