Class yii\bootstrap\Progress
Inheritance | yii\bootstrap\Progress » yii\bootstrap\Widget » yii\base\Widget |
---|---|
Uses Traits | yii\bootstrap\BootstrapWidgetTrait |
Available since extension's version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-bootstrap/blob/master/src/Progress.php |
Progress renders a bootstrap progress bar component.
For example,
// default with label echo Progress::widget([ 'percent' => 60, 'label' => 'test', ]); // styled echo Progress::widget([ 'percent' => 65, 'barOptions' => ['class' => 'progress-bar-danger'] ]); // striped echo Progress::widget([ 'percent' => 70, 'barOptions' => ['class' => 'progress-bar-warning'], 'options' => ['class' => 'progress-striped'] ]); // striped animated echo Progress::widget([ 'percent' => 70, 'barOptions' => ['class' => 'progress-bar-success'], 'options' => ['class' => 'active progress-striped'] ]); // stacked bars echo Progress::widget([ 'bars' => [ ['percent' => 30, 'options' => ['class' => 'progress-bar-danger']], ['percent' => 30, 'label' => 'test', 'options' => ['class' => 'progress-bar-success']], ['percent' => 35, 'options' => ['class' => 'progress-bar-warning']], ] ]);
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$barOptions | array | The HTML attributes of the bar. | yii\bootstrap\Progress |
$bars | array | A set of bars that are stacked together to form a single progress bar. | yii\bootstrap\Progress |
$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 |
$label | string | The button label. | yii\bootstrap\Progress |
$options | array | The HTML attributes for the widget container tag. | yii\bootstrap\Widget |
$percent | integer | The amount of progress as a percentage. | yii\bootstrap\Progress |
Public Methods
Method | Description | Defined By |
---|---|---|
getView() | yii\bootstrap\BootstrapWidgetTrait | |
init() | Initializes the widget. | yii\bootstrap\Progress |
run() | Renders the widget. | yii\bootstrap\Progress |
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 |
renderBar() | Generates a bar | yii\bootstrap\Progress |
renderProgress() | Renders the progress. | yii\bootstrap\Progress |
Property Details
The HTML attributes of the bar.
See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
A set of bars that are stacked together to form a single progress bar. Each bar is an array of the following structure:
[ // required, the amount of progress as a percentage. 'percent' => 30, // optional, the label to be displayed on the bar 'label' => '30%', // optional, array, additional HTML attributes for the bar tag 'options' => [], ]
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.
If you override this method, make sure you call the parent implementation first.
public void init ( ) |
public function init() { parent::init(); Html::addCssClass($this->options, ['widget' => 'progress']); }
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(); }
Generates a bar
protected string renderBar ( $percent, $label = '', $options = [] ) | ||
$percent | integer | The percentage of the bar |
$label | string | , optional, the label to display at the bar |
$options | array | The HTML attributes of the bar |
return | string | The rendering result. |
---|
protected function renderBar($percent, $label = '', $options = []) { $defaultOptions = [ 'role' => 'progressbar', 'aria-valuenow' => $percent, 'aria-valuemin' => 0, 'aria-valuemax' => 100, 'style' => "width:{$percent}%", ]; $options = array_merge($defaultOptions, $options); Html::addCssClass($options, ['widget' => 'progress-bar']); $out = Html::beginTag('div', $options); $out .= $label; $out .= Html::tag('span', \Yii::t('yii', '{percent}% Complete', ['percent' => $percent]), [ 'class' => 'sr-only' ]); $out .= Html::endTag('div'); return $out; }
Renders the progress.
protected string renderProgress ( ) | ||
return | string | The rendering result. |
---|---|---|
throws | \yii\base\InvalidConfigException | if the "percent" option is not set in a stacked progress bar. |
protected function renderProgress() { if (empty($this->bars)) { return $this->renderBar($this->percent, $this->label, $this->barOptions); } $bars = []; foreach ($this->bars as $bar) { $label = ArrayHelper::getValue($bar, 'label', ''); if (!isset($bar['percent'])) { throw new InvalidConfigException("The 'percent' option is required."); } $options = ArrayHelper::getValue($bar, 'options', []); $bars[] = $this->renderBar($bar['percent'], $label, $options); } return implode("\n", $bars); }
Renders the widget.
public void run ( ) |
public function run() { BootstrapAsset::register($this->getView()); return implode("\n", [ Html::beginTag('div', $this->options), $this->renderProgress(), Html::endTag('div') ]) . "\n"; }