forked from 2amigos/yii2-multi-select-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiSelect.php
79 lines (70 loc) · 1.97 KB
/
MultiSelect.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
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* @copyright Copyright (c) 2013 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\multiselect;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
use yii\base\InvalidConfigException;
use Yii;
/**
* MultiSelect renders a [David Stutz Multiselect widget](http://davidstutz.github.io/bootstrap-multiselect/)
*
* @see http://davidstutz.github.io/bootstrap-multiselect/
* @author Antonio Ramirez <[email protected]>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\widgets
*/
class MultiSelect extends InputWidget
{
/**
* @var array data for generating the list options (value=>display)
*/
public $data = [];
/**
* @var array the options for the Bootstrap Multiselect JS plugin.
* Please refer to the Bootstrap Multiselect plugin Web page for possible options.
* @see http://davidstutz.github.io/bootstrap-multiselect/#options
*/
public $clientOptions = [];
/**
* Initializes the widget.
*/
public function init()
{
if (empty($this->data)) {
throw new InvalidConfigException('"Multiselect::$data" attribute cannot be blank or an empty array.');
}
parent::init();
}
/**
* @inheritdoc
*/
public function run()
{
if ($this->hasModel()) {
echo Html::activeDropDownList($this->model, $this->attribute, $this->data, $this->options);
} else {
echo Html::dropDownList($this->name, $this->value, $this->data, $this->options);
}
$this->registerPlugin();
}
/**
* Registers MultiSelect Bootstrap plugin and the related events
*/
protected function registerPlugin()
{
$view = $this->getView();
MultiSelectAsset::register($view);
$id = $this->options['id'];
$options = $this->clientOptions !== false && !empty($this->clientOptions)
? Json::encode($this->clientOptions)
: '';
$js = "jQuery('#$id').multiselect($options);";
$view->registerJs($js);
}
}