Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ToggleButtonGroup.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\base\InvalidConfigException;
  9. /**
  10. * ToggleButtonGroup allows rendering form inputs Checkbox/Radio toggle button groups.
  11. *
  12. * You can use this widget in an [[yii\bootstrap\ActiveForm|ActiveForm]] using the [[yii\widgets\ActiveField::widget()|widget()]]
  13. * method, for example like this:
  14. *
  15. * ```php
  16. * <?= $form->field($model, 'item_id')->widget(\yii\bootstrap\ToggleButtonGroup::classname(), [
  17. * // configure additional widget properties here
  18. * ]) ?>
  19. * ```
  20. *
  21. * @see http://getbootstrap.com/javascript/#buttons-checkbox-radio
  22. *
  23. * @author Paul Klimov <klimov.paul@gmail.com>
  24. * @since 2.0.6
  25. */
  26. class ToggleButtonGroup extends InputWidget
  27. {
  28. /**
  29. * @var string input type, can be:
  30. * - 'checkbox'
  31. * - 'radio'
  32. */
  33. public $type;
  34. /**
  35. * @var array the data item used to generate the checkboxes.
  36. * The array values are the labels, while the array keys are the corresponding checkbox or radio values.
  37. */
  38. public $items = [];
  39. /**
  40. * @var array, the HTML attributes for the label (button) tag.
  41. * @see Html::checkbox()
  42. * @see Html::radio()
  43. */
  44. public $labelOptions = [];
  45. /**
  46. * @var boolean whether the items labels should be HTML-encoded.
  47. */
  48. public $encodeLabels = true;
  49. /**
  50. * @inheritdoc
  51. */
  52. public function init()
  53. {
  54. parent::init();
  55. $this->registerPlugin('button');
  56. Html::addCssClass($this->options, 'btn-group');
  57. $this->options['data-toggle'] = 'buttons';
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function run()
  63. {
  64. if (!isset($this->options['item'])) {
  65. $this->options['item'] = [$this, 'renderItem'];
  66. }
  67. switch ($this->type) {
  68. case 'checkbox':
  69. return Html::activeCheckboxList($this->model, $this->attribute, $this->items, $this->options);
  70. case 'radio':
  71. return Html::activeRadioList($this->model, $this->attribute, $this->items, $this->options);
  72. default:
  73. throw new InvalidConfigException("Unsupported type '{$this->type}'");
  74. }
  75. }
  76. /**
  77. * Default callback for checkbox/radio list item rendering.
  78. * @param integer $index item index.
  79. * @param string $label item label.
  80. * @param string $name input name.
  81. * @param boolean $checked whether value is checked or not.
  82. * @param string $value input value.
  83. * @return string generated HTML.
  84. * @see Html::checkbox()
  85. * @see Html::radio()
  86. */
  87. public function renderItem($index, $label, $name, $checked, $value)
  88. {
  89. $labelOptions = $this->labelOptions;
  90. Html::addCssClass($labelOptions, 'btn');
  91. if ($checked) {
  92. Html::addCssClass($labelOptions, 'active');
  93. }
  94. $type = $this->type;
  95. if ($this->encodeLabels) {
  96. $label = Html::encode($label);
  97. }
  98. return Html::$type($name, $checked, ['label' => $label, 'labelOptions' => $labelOptions, 'value' => $value]);
  99. }
  100. }