Class yii\bootstrap4\ToggleButtonGroup
Inheritance | yii\bootstrap4\ToggleButtonGroup » yii\bootstrap4\InputWidget » yii\widgets\InputWidget |
---|---|
Uses Traits | yii\bootstrap4\BootstrapWidgetTrait |
Source Code | https://github.com/yiisoft/yii2-bootstrap4/blob/master/src/ToggleButtonGroup.php |
ToggleButtonGroup allows rendering form inputs Checkbox/Radio toggle button groups.
You can use this widget in an ActiveForm using the yii\widgets\ActiveField::widget() method, for example like this:
<?= $form->field($model, 'item_id')->widget(\yii\bootstrap4\ToggleButtonGroup::class, [
'type' => \yii\bootstrap4\ToggleButtonGroup::TYPE_CHECKBOX,
'items' => [
'fooValue' => 'BarLabel',
'barValue' => 'BazLabel'
]
]) ?>
See also https://getbootstrap.com/docs/4.5/components/buttons/#checkbox-and-radio-buttons.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$clientEvents | array | The event handlers for the underlying Bootstrap JS plugin. | yii\bootstrap4\BootstrapWidgetTrait |
$clientOptions | array | The options for the underlying Bootstrap JS plugin. | yii\bootstrap4\BootstrapWidgetTrait |
$encodeLabels | boolean | Whether the items labels should be HTML-encoded. | yii\bootstrap4\ToggleButtonGroup |
$items | array | The data item used to generate the checkboxes. | yii\bootstrap4\ToggleButtonGroup |
$labelOptions | yii\bootstrap4\ToggleButtonGroup | ||
$type | string | Input type, can be TYPE_CHECKBOX or TYPE_RADIO | yii\bootstrap4\ToggleButtonGroup |
Public Methods
Method | Description | Defined By |
---|---|---|
getView() | yii\bootstrap4\BootstrapWidgetTrait | |
init() | Initializes the widget. | yii\bootstrap4\ToggleButtonGroup |
renderItem() | Default callback for checkbox/radio list item rendering. | yii\bootstrap4\ToggleButtonGroup |
run() | yii\bootstrap4\ToggleButtonGroup |
Protected Methods
Method | Description | Defined By |
---|---|---|
registerClientEvents() | Registers JS event handlers that are listed in $clientEvents. | yii\bootstrap4\BootstrapWidgetTrait |
registerPlugin() | Registers a specific Bootstrap plugin and the related events | yii\bootstrap4\BootstrapWidgetTrait |
Constants
Constant | Value | Description | Defined By |
---|---|---|---|
TYPE_CHECKBOX | 'checkbox' | Checkbox type | yii\bootstrap4\ToggleButtonGroup |
TYPE_RADIO | 'radio' | Radio type | yii\bootstrap4\ToggleButtonGroup |
Property Details
Whether the items labels should be HTML-encoded.
The data item used to generate the checkboxes. The array values are the labels, while the array keys are the corresponding checkbox or radio values.
See also:
- \yii\bootstrap4\Html::checkbox()
- \yii\bootstrap4\Html::radio()
Method Details
public abstract \yii\web\View getView ( ) | ||
return | \yii\web\View |
The view object that can be used to render views or view files. |
---|
abstract function getView();
Initializes the widget.
This method will register the bootstrap asset bundle. If you override this method, make sure you call the parent implementation first.
public void init ( ) |
public function init()
{
parent::init();
$this->registerPlugin('button');
Html::addCssClass($this->options, ['widget' => 'btn-group-toggle']);
$this->options['data-toggle'] = 'buttons';
}
Defined in: yii\bootstrap4\BootstrapWidgetTrait::registerClientEvents()
Registers JS event handlers that are listed in $clientEvents.
protected void registerClientEvents ( ) |
protected function registerClientEvents()
{
if (!empty($this->clientEvents)) {
$id = $this->options['id'];
$js = [];
foreach ($this->clientEvents as $event => $handler) {
$js[] = "jQuery('#$id').on('$event', $handler);";
}
$this->getView()->registerJs(implode("\n", $js));
}
}
Defined in: yii\bootstrap4\BootstrapWidgetTrait::registerPlugin()
Registers a specific Bootstrap plugin and the related events
protected void registerPlugin ( $name ) | ||
$name | string |
The name of the Bootstrap plugin |
protected function registerPlugin($name)
{
$view = $this->getView();
BootstrapPluginAsset::register($view);
$id = $this->options['id'];
if ($this->clientOptions !== false) {
$options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions);
$js = "jQuery('#$id').$name($options);";
$view->registerJs($js);
}
$this->registerClientEvents();
}
Default callback for checkbox/radio list item rendering.
See also:
- \yii\bootstrap4\Html::checkbox()
- \yii\bootstrap4\Html::radio()
public string renderItem ( $index, $label, $name, $checked, $value ) | ||
$index | integer |
Item index. |
$label | string |
Item label. |
$name | string |
Input name. |
$checked | boolean |
Whether value is checked or not. |
$value | string |
Input value. |
return | string |
Generated HTML. |
---|
public function renderItem($index, $label, $name, $checked, $value)
{
$labelOptions = $this->labelOptions;
$labelOptions['wrapInput'] = true;
Html::addCssClass($labelOptions, ['widget' => 'btn']);
if ($checked) {
Html::addCssClass($labelOptions, ['activate' => 'active']);
}
$type = $this->type;
if ($this->encodeLabels) {
$label = Html::encode($label);
}
return Html::$type($name, $checked, [
'label' => $label,
'labelOptions' => $labelOptions,
'autocomplete' => 'off',
'value' => $value,
'id' => Html::getInputIdByName($name) . '-' . $index
]);
}
public void run ( ) | ||
throws | \yii\base\InvalidConfigException |
---|
public function run()
{
if (!isset($this->options['item'])) {
$this->options['item'] = [$this, 'renderItem'];
}
switch ($this->type) {
case 'checkbox':
if ($this->hasModel()) {
return Html::activeCheckboxList($this->model, $this->attribute, $this->items, $this->options);
} else {
return Html::checkboxList($this->name, $this->value, $this->items, $this->options);
}
case 'radio':
if ($this->hasModel()) {
return Html::activeRadioList($this->model, $this->attribute, $this->items, $this->options);
} else {
return Html::radioList($this->name, $this->value, $this->items, $this->options);
}
default:
throw new InvalidConfigException("Unsupported type '{$this->type}'");
}
}