-
Notifications
You must be signed in to change notification settings - Fork 1
/
DualListBox.php
executable file
·135 lines (103 loc) · 3.71 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
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
<?php
namespace amilna\yap;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
use maksyutin\duallistbox\Asset;
/**
* This is just an example.
*/
class DualListBox extends InputWidget
{
public $nametitle;
public $title;
public $lngOptions;
public $attributes;
public $data;
public $data_id;
public $data_value;
public function init()
{
parent::init();
$this->data_id = isset($this->data_id) ? $this->data_id : 'id';
$this->data_value = isset($this->data_value) ? $this->data_value : 'name';
$this->nametitle = isset($this->nametitle) ? $this->nametitle : '';
$this->registerAssets();
echo Html::activeTextInput($this->model, $this->attribute, ['class' => 'hidden', 'value' => $this->value]);
}
public function run()
{
$view = $this->getView();
$model = $this->model;
$inputId = $this->attribute;
$selected = \yii\helpers\Json::decode($this->model->$inputId, JSON_UNESCAPED_UNICODE);
$selected = ($selected == null) ? [] : $selected;
$json_sel = Json::encode($selected);
$idModel = strtolower($model->formName());
$this->attributes = $this->model->attributes();
$data = ($this->data) ? $this->data->asArray()->all() : [];
$css = "button.str {margin-bottom:10px!important;}";
$view->registerCss($css);
echo '<div id="'.$inputId.'" >';
$ret_sel = '';
$ret = '<select style="display: none;" multiple = "multiple">';
$cnt = 0;
foreach ($data as $key => $value) {
if (!in_array($value[$this->data_id], $selected)) {
$ret .= '<option value="' . $value[$this->data_id] . '">' . $value[$this->data_value] . '</option>' . "\n";
} else {
$cnt++;
$ret_sel .= '$("#dlb-'.$this->attribute.' .selected").
append("<option value=' . $value[$this->data_id] . '>' . $value[$this->data_value] . '</option>");';
}
}
$ret .= '</select>';
$lng_opt = new Json();
$lng_opt->warning_info = 'Are you sure you want to move this many items?
Doing so can cause your browser to become unresponsive.';
$lng_opt->search_placeholder = 'Filter';
$lng_opt->showing = '- showing';
$lng_opt->available = 'Available';
$lng_opt->selected = 'Selected';
foreach($lng_opt as $key=>$value) {
$lng_opt->$key = isset($this->lngOptions[$key]) ? $this->lngOptions[$key] : $value;
}
$options = 'lngOptions: '. json_encode($lng_opt);
$modelId = strtolower($idModel."-".$inputId);
$js = <<<SCRIPT
$('#$inputId').DualListBox({
json: false,
name: '$idModel',
id: $inputId,
title: '$this->nametitle',
$options
});
$ret_sel
$("#$idModel-$inputId").val('$json_sel');
$('#dlb-$inputId .selected-count').text('$cnt');
$(".selected").bind("DOMSubtreeModified",function(){
var optionValues = [];
$('.selected option').each(function() { optionValues.push($(this).val()); });
$('#$modelId').val(JSON.stringify(optionValues));
//console.log($('#$modelId').val());
});
SCRIPT;
$view->registerJs($js);
return $ret.'</div>';
}
/**
* Registers the needed assets
*/
public function registerAssets()
{
$view = $this->getView();
Asset::register($view);
//
// $attr = $this->attribute;
// $js = <<<SCRIPT
//
//SCRIPT;
//
// $view->registerJs($js);
}
}