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