Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ButtonDropdown.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. use yii\helpers\Html;
  10. /**
  11. * ButtonDropdown renders a group or split button dropdown bootstrap component.
  12. *
  13. * For example,
  14. *
  15. * ```php
  16. * // a button group using Dropdown widget
  17. * echo ButtonDropdown::widget([
  18. * 'label' => 'Action',
  19. * 'dropdown' => [
  20. * 'items' => [
  21. * ['label' => 'DropdownA', 'url' => '/'],
  22. * ['label' => 'DropdownB', 'url' => '#'],
  23. * ],
  24. * ],
  25. * ]);
  26. * ```
  27. * @see http://getbootstrap.com/javascript/#buttons
  28. * @see http://getbootstrap.com/components/#btn-dropdowns
  29. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  30. * @since 2.0
  31. */
  32. class ButtonDropdown extends Widget
  33. {
  34. /**
  35. * @var string the button label
  36. */
  37. public $label = 'Button';
  38. /**
  39. * @var array the HTML attributes for the container tag. The following special options are recognized:
  40. *
  41. * - tag: string, defaults to "div", the name of the container tag.
  42. *
  43. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  44. * @since 2.0.1
  45. */
  46. public $containerOptions = [];
  47. /**
  48. * @var array the HTML attributes of the button.
  49. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  50. */
  51. public $options = [];
  52. /**
  53. * @var array the configuration array for [[Dropdown]].
  54. */
  55. public $dropdown = [];
  56. /**
  57. * @var boolean whether to display a group of split-styled button group.
  58. */
  59. public $split = false;
  60. /**
  61. * @var string the tag to use to render the button
  62. */
  63. public $tagName = 'button';
  64. /**
  65. * @var boolean whether the label should be HTML-encoded.
  66. */
  67. public $encodeLabel = true;
  68. /**
  69. * Renders the widget.
  70. */
  71. public function run()
  72. {
  73. Html::addCssClass($this->containerOptions, 'btn-group');
  74. $options = $this->containerOptions;
  75. $tag = ArrayHelper::remove($options, 'tag', 'div');
  76. $this->registerPlugin('button');
  77. return implode("\n", [
  78. Html::beginTag($tag, $this->containerOptions),
  79. $this->renderButton(),
  80. $this->renderDropdown(),
  81. Html::endTag($tag)
  82. ]);
  83. }
  84. /**
  85. * Generates the button dropdown.
  86. * @return string the rendering result.
  87. */
  88. protected function renderButton()
  89. {
  90. Html::addCssClass($this->options, 'btn');
  91. $label = $this->label;
  92. if ($this->encodeLabel) {
  93. $label = Html::encode($label);
  94. }
  95. if ($this->split) {
  96. $options = $this->options;
  97. $this->options['data-toggle'] = 'dropdown';
  98. Html::addCssClass($this->options, 'dropdown-toggle');
  99. $splitButton = Button::widget([
  100. 'label' => '<span class="caret"></span>',
  101. 'encodeLabel' => false,
  102. 'options' => $this->options,
  103. 'view' => $this->getView(),
  104. ]);
  105. } else {
  106. $label .= ' <span class="caret"></span>';
  107. $options = $this->options;
  108. if (!isset($options['href'])) {
  109. $options['href'] = '#';
  110. }
  111. Html::addCssClass($options, 'dropdown-toggle');
  112. $options['data-toggle'] = 'dropdown';
  113. $splitButton = '';
  114. }
  115. return Button::widget([
  116. 'tagName' => $this->tagName,
  117. 'label' => $label,
  118. 'options' => $options,
  119. 'encodeLabel' => false,
  120. 'view' => $this->getView(),
  121. ]) . "\n" . $splitButton;
  122. }
  123. /**
  124. * Generates the dropdown menu.
  125. * @return string the rendering result.
  126. */
  127. protected function renderDropdown()
  128. {
  129. $config = $this->dropdown;
  130. $config['clientOptions'] = false;
  131. $config['view'] = $this->getView();
  132. return Dropdown::widget($config);
  133. }
  134. }