RadioButtonColumn.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\grid;
  8. use Closure;
  9. use yii\base\InvalidConfigException;
  10. use yii\helpers\Html;
  11. /**
  12. * RadioButtonColumn displays a column of radio buttons in a grid view.
  13. *
  14. * To add a RadioButtonColumn to the [[GridView]], add it to the [[GridView::columns|columns]] configuration as follows:
  15. *
  16. * ```php
  17. * 'columns' => [
  18. * // ...
  19. * [
  20. * 'class' => 'yii\grid\RadioButtonColumn',
  21. * 'radioOptions' => function ($model) {
  22. * return [
  23. * 'value' => $model['value'],
  24. * 'checked' => $model['value'] == 2
  25. * ];
  26. * }
  27. * ],
  28. * ]
  29. * ```
  30. *
  31. * @author Kirk Hansen <hanski07@luther.edu>
  32. * @since 2.0.11
  33. */
  34. class RadioButtonColumn extends Column
  35. {
  36. /**
  37. * @var string the name of the input radio button input fields.
  38. */
  39. public $name = 'radioButtonSelection';
  40. /**
  41. * @var array|\Closure the HTML attributes for the radio buttons. This can either be an array of
  42. * attributes or an anonymous function ([[Closure]]) returning such an array.
  43. *
  44. * The signature of the function should be as follows: `function ($model, $key, $index, $column)`
  45. * where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
  46. * and `$column` is a reference to the [[RadioButtonColumn]] object.
  47. *
  48. * A function may be used to assign different attributes to different rows based on the data in that row.
  49. * Specifically if you want to set a different value for the radio button you can use this option
  50. * in the following way (in this example using the `name` attribute of the model):
  51. *
  52. * ```php
  53. * 'radioOptions' => function ($model, $key, $index, $column) {
  54. * return ['value' => $model->attribute];
  55. * }
  56. * ```
  57. *
  58. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  59. */
  60. public $radioOptions = [];
  61. /**
  62. * {@inheritdoc}
  63. * @throws \yii\base\InvalidConfigException if [[name]] is not set.
  64. */
  65. public function init()
  66. {
  67. parent::init();
  68. if (empty($this->name)) {
  69. throw new InvalidConfigException('The "name" property must be set.');
  70. }
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function renderDataCellContent($model, $key, $index)
  76. {
  77. if ($this->content !== null) {
  78. return parent::renderDataCellContent($model, $key, $index);
  79. }
  80. if ($this->radioOptions instanceof Closure) {
  81. $options = call_user_func($this->radioOptions, $model, $key, $index, $this);
  82. } else {
  83. $options = $this->radioOptions;
  84. if (!isset($options['value'])) {
  85. $options['value'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $key;
  86. }
  87. }
  88. $checked = isset($options['checked']) ? $options['checked'] : false;
  89. return Html::radio($this->name, $checked, $options);
  90. }
  91. }