-
Notifications
You must be signed in to change notification settings - Fork 10
/
DualListbox.php
66 lines (55 loc) · 1.5 KB
/
DualListbox.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
59
60
61
62
63
64
65
66
<?php
namespace softark\duallistbox;
use yii\widgets\InputWidget;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* yii2-dual-listbox Widget
*
* @author Nobuo Kihara <softark@gmail.comu>
* @since 1.0
*/
class DualListbox extends InputWidget
{
/**
* @var array listbox items
*/
public $items = [];
/**
* @var string|array selected items
*/
public $selection;
/**
* @var array listbox options
*/
public $options = [];
/**
* @var array dual listbox options
*/
public $clientOptions = [];
/**
* @inheritdoc
*/
public function run()
{
$this->registerClientScript();
Html::addCssClass($this->options, 'form-control');
$this->options['multiple'] = true;
if ($this->hasModel()) {
return Html::activeListBox($this->model, $this->attribute, $this->items, $this->options);
} else {
return Html::listBox($this->name, $this->selection, $this->items, $this->options);
}
}
/**
* Registers the required JavaScript.
*/
public function registerClientScript()
{
$view = $this->getView();
DualListboxAsset::register($view);
$id = (array_key_exists('id', $this->options)) ? $this->options['id'] : Html::getInputId($this->model, $this->attribute);
$options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
$view->registerJs("jQuery('#$id').bootstrapDualListbox($options);");
}
}