/
MultiSelectWidget.php
58 lines (45 loc) · 1.52 KB
/
MultiSelectWidget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace gerpayt\yii2_multiselect;
use yii\helpers\ArrayHelper;
use yii\helpers\BaseHtml;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\widgets\InputWidget;
use gerpayt\yii2_ueditor\UEditorAsset;
class MultiSelectWidget extends InputWidget
{
public function run()
{
$this->view->registerAssetBundle(MultiSelectAsset::className());
$options = $this->options;
if (isset($options['id'])) {
$id = $options['id'];
} else {
$id = BaseHtml::getInputId($this->model, $this->attribute);
}
if (isset($options['name'])) {
$name = $options['name'];
} elseif ($this->hasModel()) {
$name = BaseHtml::getInputName($this->model, $this->attribute);
} else {
$name = $this->name;
}
// if (isset($options['value'])) {
// $value = $options['value'];
// } elseif ($this->hasModel()) {
// $value = BaseHtml::getAttributeValue($this->model, $this->attribute);
// } else {
// $value = $this->value;
// }
$len = strlen($this->attribute);
$widget = Html::beginTag('div', ['id' => $id, 'name' => $name]);
foreach ($this->model as $k => $v) {
if (substr($k,0,$len+1) == $this->attribute.'_') {
$widget .= Html::activeCheckbox($this->model, $k, ['labelOptions'=>['class'=>'checkbox-inline']]);
}
}
$widget .= Html::endTag('div');
echo $widget;
}
}