-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelect2Widget.php
300 lines (284 loc) · 7.94 KB
/
Select2Widget.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
namespace indicalabs\select2;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\Json;
use yii\web\JsExpression;
/**
* Wrapper Widget to use jQuery Select2 in Yii2 application.
*
* @author Venu Narukulla. Venu <[email protected]>
* @copyright Copyright © 2013
* @package extensions
* @subpackage select2
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @version 1.0 rev.0
* IN EXTENSIONS.PHP
* 'yiisoft/yii2-select2' =>
* array (
* 'name' => 'indicalabs/yii2-select2',
* 'version' => '9999999-dev',
* 'alias' =>
* array (
* '@yii/select2' => $vendorDir . '/indicalabs/yii2-select2/yii/select2',
* ),
* ),
* <?= $form->field($model,'country_id')->widget(yii\select2\Select2::className(),[
* 'model'=>$model,
* 'attribute'=>'country_id',
* 'language' => 'ru',
* // 'multiple' => false, // multiple option is working only in case of non ajax
* // 'data' => [
* // 1 => 'Option 1',
* // 2 => 'Option 2',
* // 3 => 'Option 3',
* // 4 => 'Option 4',
* // ],
* //
* 'clientOptions' => [
* 'allowClear' => true,
* 'placeholder' => 'Please Select a Value',
* 'width' => '100%',
* 'ajax' => [
* 'url' => Yii::$app->urlManager->createAbsoluteUrl('data/caste/findcountry'),
* 'dataType' => 'json',
* 'data' => new yii\web\JsExpression('function (term, page) {
* return {
* term: term, // search term
* page: page,
* page_limit: 10,
* };
* }'),
* 'results' => new yii\web\JsExpression('function (data, page) { // parse the results into the format expected by Select2.
* return {
* results: data
* };
* }')
* ],
* ],
* ]) ?>
* ```
*
* in your Ajax Controller
* ```
* class AjaxController extends Controller
* {
* public function actionFindcountry($term)
* {
* $term = $_GET['term'];
* // echo "term---".$term;
* $countries = Country::findBySql("select id, country_name as text from tbl_country where country_name LIKE :name",array(':name'=>$term.'%'))
* ->asArray()
* ->all();
* echo json_encode($countries);
* }
*
* }
* @see https://github.com/ivaynberg/select2 jQuery Select2
*/
class Select2Widget extends \yii\widgets\InputWidget
{
/** Name of inline JavaScript package that is registered by the widget */
const INLINE_JS_KEY = 'indicalabs/select2/';
/**
* Points to use Bootstrap theme
* @var boolean
*/
public $bootstrap = true;
/**
* Language code
* Set False to disable
* @var string | boolean
*/
public $language;
/**
* Array data
* @example [['id'=>1, 'text'=>'enhancement'], ['id'=>2, 'text'=>'bug']]
* @var array
*/
public $data;
/**
* You can use Select2Action to provide AJAX data
* @see \yii\helpers\BaseUrl::to()
* @var array|string
*/
public $ajax;
/**
* @see \yii\helpers\BaseArrayHelper::map()
* @var array
*/
public $items = [];
/**
* A placeholder value can be defined and will be displayed until a selection is made
* @var string
*/
public $placeholder;
/**
* Multiple select boxes
* @var boolean
*/
public $multiple;
/**
* Tagging support
* @var boolean
*/
public $tags;
/**
* @link https://select2.github.io/options.html
* @var array
*/
public $settings = [];
/**
* Events array. Array keys are the events name, and array values are the events callbacks.
* Example:
* ```php
* [
* 'select2-open' => 'function (e) { log("select2:open", e); }',
* 'select2-close' => new JsExpression('function (e) { log("select2:close", e); }'),
* 'select2-select' => [
* 'function (e) { log("select2:select", e); }',
* 'function (e) { console.log(e); }'
* ]
* ]
* ```
* @var array Plugin events
*/
public $events = [];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->tags) {
$this->options['data-tags'] = 'true';
$this->options['multiple'] = true;
}
if ($this->language) {
$this->options['data-language'] = $this->language;
}
if (!is_null($this->ajax)) {
$this->options['data-ajax--url'] = Url::to($this->ajax);
$this->options['data-ajax--cache'] = 'true';
}
if ($this->placeholder) {
$this->options['data-placeholder'] = $this->placeholder;
}
if ($this->multiple) {
$this->options['data-multiple'] = 'true';
$this->options['multiple'] = true;
}
if (!empty($this->data)) {
$this->options['data-data'] = \yii\helpers\Json::encode($this->data);
}
if (!isset($this->options['class'])) {
$this->options['class'] = 'form-control';
}
if ($this->bootstrap) {
$this->options['data-theme'] = 'bootstrap';
}
if ($this->multiple || !empty($this->settings['multiple'])) {
if ($this->hasModel()) {
$name = isset($this->options['name']) ? $this->options['name'] : Html::getInputName($this->model, $this->attribute);
} else {
$name = $this->name;
}
if (substr($name, -2) != '[]') {
$this->options['name'] = $this->name = $name . '[]';
}
}
}
/**
* @inheritdoc
*/
public function run()
{
$this->registerAssets();
if ($this->hasModel()) {
echo Html::activeDropDownList($this->model, $this->attribute, $this->items, $this->options);
} else {
echo Html::dropDownList($this->name, $this->value, $this->items, $this->options);
}
}
/**
* Registers Assets
*/
public function registerAssets()
{
$view = $this->getView();
$bandle = Select2Asset::register($view);
if ($this->language !== false) {
$langs[0] = $this->language ? $this->language : \Yii::$app->language;
if (($pos = strpos($langs[0], '-')) > 0) {
// If "en-us" is not found, try to use "en".
$langs[1] = substr($langs[0], 0, $pos);
}
foreach ($langs as $lang) {
$langFile = "/js/i18n/{$lang}.js";
if (file_exists($bandle->sourcePath . $langFile)) {
$view->registerJsFile($bandle->baseUrl . $langFile, ['depends' => Select2Asset::className()]);
break;
}
}
}
if ($this->bootstrap) {
Select2BootstrapAsset::register($view);
}
$settings = Json::encode($this->settings);
$view->registerJs("jQuery('#{$this->options['id']}').select2($settings);", $view::POS_READY, self::INLINE_JS_KEY . $this->options['id']);
// Register events
$this->registerEvents();
}
/**
* Register plugin' events.
*/
protected function registerEvents()
{
$view = $this->getView();
$selector = '#' . $this->options['id'];
if (!empty($this->events)) {
$js = [];
foreach ($this->events as $event => $callback) {
if (is_array($callback)) {
foreach ($callback as $function) {
if (!$function instanceof JsExpression) {
$function = new JsExpression($function);
}
$js[] = "jQuery('$selector').on('$event', $function);";
}
} else {
if (!$callback instanceof JsExpression) {
$callback = new JsExpression($callback);
}
$js[] = "jQuery('$selector').on('$event', $callback);";
}
}
if (!empty($js)) {
$js = implode("\n", $js);
$view->registerJs($js, $view::POS_READY, self::INLINE_JS_KEY . 'events/' . $this->options['id']);
}
}
}
/**
* Renders the AutoComplete widget.
* @return string the rendering result.
public function renderWidget()
{
$contents = [];
if (isset($this->clientOptions['ajax'])) {
if (isset($this->model)) {
$contents[] = Html::activeTextInput($this->model, $this->attribute, $this->options);
} else {
$contents[] = Html::textInput($this->name, $this->value, $this->options);
}
} else {
if (isset($this->model)) {
$contents[] = Html::activeDropDownList($this->model, $this->attribute, $this->data, $this->options);
} else {
$contents[] = Html::dropDownList($this->name, $this->value, $this->data, $this->options);
}
}
return implode("\n", $contents);
}
*/
}