You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ButtonGroup.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\bootstrap;
  8. use yii\helpers\ArrayHelper;
  9. /**
  10. * ButtonGroup renders a button group bootstrap component.
  11. *
  12. * For example,
  13. *
  14. * ```php
  15. * // a button group with items configuration
  16. * echo ButtonGroup::widget([
  17. * 'buttons' => [
  18. * ['label' => 'A'],
  19. * ['label' => 'B'],
  20. * ['label' => 'C', 'visible' => false],
  21. * ]
  22. * ]);
  23. *
  24. * // button group with an item as a string
  25. * echo ButtonGroup::widget([
  26. * 'buttons' => [
  27. * Button::widget(['label' => 'A']),
  28. * ['label' => 'B'],
  29. * ]
  30. * ]);
  31. * ```
  32. *
  33. * Pressing on the button should be handled via JavaScript. See the following for details:
  34. *
  35. * @see http://getbootstrap.com/javascript/#buttons
  36. * @see http://getbootstrap.com/components/#btn-groups
  37. *
  38. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  39. * @since 2.0
  40. */
  41. class ButtonGroup extends Widget
  42. {
  43. /**
  44. * @var array list of buttons. Each array element represents a single button
  45. * which can be specified as a string or an array of the following structure:
  46. *
  47. * - label: string, required, the button label.
  48. * - options: array, optional, the HTML attributes of the button.
  49. * - visible: boolean, optional, whether this button is visible. Defaults to true.
  50. */
  51. public $buttons = [];
  52. /**
  53. * @var boolean whether to HTML-encode the button labels.
  54. */
  55. public $encodeLabels = true;
  56. /**
  57. * Initializes the widget.
  58. * If you override this method, make sure you call the parent implementation first.
  59. */
  60. public function init()
  61. {
  62. parent::init();
  63. Html::addCssClass($this->options, ['widget' => 'btn-group']);
  64. }
  65. /**
  66. * Renders the widget.
  67. */
  68. public function run()
  69. {
  70. BootstrapAsset::register($this->getView());
  71. return Html::tag('div', $this->renderButtons(), $this->options);
  72. }
  73. /**
  74. * Generates the buttons that compound the group as specified on [[buttons]].
  75. * @return string the rendering result.
  76. */
  77. protected function renderButtons()
  78. {
  79. $buttons = [];
  80. foreach ($this->buttons as $button) {
  81. if (is_array($button)) {
  82. $visible = ArrayHelper::remove($button, 'visible', true);
  83. if ($visible === false) {
  84. continue;
  85. }
  86. $button['view'] = $this->getView();
  87. if (!isset($button['encodeLabel'])) {
  88. $button['encodeLabel'] = $this->encodeLabels;
  89. }
  90. $buttons[] = Button::widget($button);
  91. } else {
  92. $buttons[] = $button;
  93. }
  94. }
  95. return implode("\n", $buttons);
  96. }
  97. }