Class yii\bootstrap\ToggleButtonGroup
Inheritance | yii\bootstrap\ToggleButtonGroup » yii\bootstrap\InputWidget » yii\widgets\InputWidget |
---|---|
Uses Traits | yii\bootstrap\BootstrapWidgetTrait |
Available since extension's version | 2.0.6 |
Source Code | https://github.com/yiisoft/yii2-bootstrap/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\bootstrap\ToggleButtonGroup::classname(), [
// configure additional widget properties here
]) ?>
See also http://getbootstrap.com/javascript/#buttons-checkbox-radio.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$clientEvents | array | The event handlers for the underlying Bootstrap JS plugin. | yii\bootstrap\BootstrapWidgetTrait |
$clientOptions | array | The options for the underlying Bootstrap JS plugin. | yii\bootstrap\BootstrapWidgetTrait |
$encodeLabels | boolean | Whether the items labels should be HTML-encoded. | yii\bootstrap\ToggleButtonGroup |
$items | array | The data item used to generate the checkboxes. | yii\bootstrap\ToggleButtonGroup |
$labelOptions | yii\bootstrap\ToggleButtonGroup | ||
$type | string | Input type, can be: - 'checkbox' - 'radio' | yii\bootstrap\ToggleButtonGroup |
Public Methods
Method | Description | Defined By |
---|---|---|
getView() | yii\bootstrap\BootstrapWidgetTrait | |
init() | Initializes the widget. | yii\bootstrap\ToggleButtonGroup |
renderItem() | Default callback for checkbox/radio list item rendering. | yii\bootstrap\ToggleButtonGroup |
run() | yii\bootstrap\ToggleButtonGroup |
Protected Methods
Method | Description | Defined By |
---|---|---|
registerClientEvents() | Registers JS event handlers that are listed in $clientEvents. | yii\bootstrap\BootstrapWidgetTrait |
registerPlugin() | Registers a specific Bootstrap plugin and the related events | yii\bootstrap\BootstrapWidgetTrait |
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\bootstrap\Html::checkbox()
- \yii\bootstrap\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, 'btn-group');
$this->options['data-toggle'] = 'buttons';
}
Defined in: yii\bootstrap\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\bootstrap\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\bootstrap\Html::checkbox()
- \yii\bootstrap\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;
Html::addCssClass($labelOptions, 'btn');
if ($checked) {
Html::addCssClass($labelOptions, 'active');
}
$type = $this->type;
if ($this->encodeLabels) {
$label = Html::encode($label);
}
return Html::$type($name, $checked, ['label' => $label, 'labelOptions' => $labelOptions, 'value' => $value]);
}
public void run ( ) |
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}'");
}
}