1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form\Field; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Facades\Admin; |
6
|
|
|
use Encore\Admin\Form\Field; |
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
class Select extends Field |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
protected $direction = "ltr"; |
14
|
|
|
|
15
|
|
|
protected static $css = [ |
16
|
|
|
'/vendor/laravel-admin/AdminLTE/plugins/select2/select2.min.css', |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
protected static $js = [ |
20
|
|
|
'/vendor/laravel-admin/AdminLTE/plugins/select2/select2.full.min.js', |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
public function render() |
24
|
|
|
{ |
25
|
|
|
if (empty($this->script)) { |
26
|
|
|
$this->script = <<<EOF |
27
|
|
|
$("{$this->getElementClassSelector()}").select2({ |
28
|
|
|
dir: "$this->direction", |
29
|
|
|
language : "$this->local", |
30
|
|
|
allowClear: true, |
31
|
|
|
placeholder: "{$this->label}" |
32
|
|
|
}); |
33
|
|
|
EOF; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($this->options instanceof \Closure) { |
37
|
|
|
if ($this->form) { |
38
|
|
|
$this->options = $this->options->bindTo($this->form->model()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->options(call_user_func($this->options, $this->value)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->options = array_filter($this->options); |
45
|
|
|
|
46
|
|
|
return parent::render()->with(['options' => $this->options]); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set options. |
51
|
|
|
* |
52
|
|
|
* @param array|callable|string $options |
53
|
|
|
* |
54
|
|
|
* @return $this|mixed |
55
|
|
|
*/ |
56
|
|
|
public function options($options = []) |
57
|
|
|
{ |
58
|
|
|
// remote options |
59
|
|
|
if (is_string($options)) { |
60
|
|
|
return call_user_func_array([ |
61
|
|
|
$this, |
62
|
|
|
'loadOptionsFromRemote' |
63
|
|
|
], func_get_args()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($options instanceof Arrayable) { |
67
|
|
|
$options = $options->toArray(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (is_callable($options)) { |
71
|
|
|
$this->options = $options; |
|
|
|
|
72
|
|
|
} else { |
73
|
|
|
$this->options = (array)$options; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set direction setting of select2. |
81
|
|
|
* |
82
|
|
|
*/ |
83
|
|
|
public function dir($dir = 'ltr') |
84
|
|
|
{ |
85
|
|
|
$this->direction = $dir; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Load options for other select on change. |
92
|
|
|
* |
93
|
|
|
* @param string $field |
94
|
|
|
* @param string $sourceUrl |
95
|
|
|
* @param string $idField |
96
|
|
|
* @param string $textField |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function load($field, $sourceUrl, $idField = 'id', $textField = 'text') |
101
|
|
|
{ |
102
|
|
|
if (Str::contains($field, '.')) { |
103
|
|
|
$field = $this->formatName($field); |
104
|
|
|
$class = str_replace([ |
105
|
|
|
'[', |
106
|
|
|
']' |
107
|
|
|
], '_', $field); |
108
|
|
|
} else { |
109
|
|
|
$class = $field; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$script = <<<EOT |
113
|
|
|
|
114
|
|
|
$(document).on('change', "{$this->getElementClassSelector()}", function () { |
115
|
|
|
var target = $(this).closest('.fields-group').find(".$class"); |
116
|
|
|
$.get("$sourceUrl?q="+this.value, function (data) { |
117
|
|
|
target.find("option").remove(); |
118
|
|
|
$(target).select2({ |
119
|
|
|
dir: "$this->direction", |
120
|
|
|
language : "$this->local", |
121
|
|
|
data: $.map(data, function (d) { |
122
|
|
|
d.id = d.$idField; |
123
|
|
|
d.text = d.$textField; |
124
|
|
|
return d; |
125
|
|
|
}) |
126
|
|
|
}).trigger('change'); |
127
|
|
|
}); |
128
|
|
|
}); |
129
|
|
|
EOT; |
130
|
|
|
|
131
|
|
|
Admin::script($script); |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Load options from remote. |
138
|
|
|
* |
139
|
|
|
* @param string $url |
140
|
|
|
* @param array $parameters |
141
|
|
|
* @param array $options |
142
|
|
|
* |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
|
|
protected function loadOptionsFromRemote($url, $parameters = [], $options = []) |
146
|
|
|
{ |
147
|
|
|
$ajaxOptions = [ |
148
|
|
|
'url' => $url . '?' . http_build_query($parameters), |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
$ajaxOptions = json_encode(array_merge($ajaxOptions, $options)); |
152
|
|
|
|
153
|
|
|
$this->script = <<<EOT |
154
|
|
|
|
155
|
|
|
$.ajax($ajaxOptions).done(function(data) { |
156
|
|
|
$("{$this->getElementClassSelector()}").select2({ |
157
|
|
|
dir: "$this->direction", |
158
|
|
|
language : "$this->local", |
159
|
|
|
data: data |
160
|
|
|
}); |
161
|
|
|
}); |
162
|
|
|
|
163
|
|
|
EOT; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Load options from ajax results. |
170
|
|
|
* |
171
|
|
|
* @param string $url |
172
|
|
|
* @param $idField |
173
|
|
|
* @param $textField |
174
|
|
|
* |
175
|
|
|
* @return $this |
176
|
|
|
*/ |
177
|
|
View Code Duplication |
public function ajax($url, $idField = 'id', $textField = 'text') |
|
|
|
|
178
|
|
|
{ |
179
|
|
|
$this->script = <<<EOT |
180
|
|
|
|
181
|
|
|
$("{$this->getElementClassSelector()}").select2({ |
182
|
|
|
dir: "$this->direction", |
183
|
|
|
language : "$this->local", |
184
|
|
|
ajax: { |
185
|
|
|
url: "$url", |
186
|
|
|
dataType: 'json', |
187
|
|
|
delay: 250, |
188
|
|
|
data: function (params) { |
189
|
|
|
return { |
190
|
|
|
q: params.term, |
191
|
|
|
page: params.page |
192
|
|
|
}; |
193
|
|
|
}, |
194
|
|
|
processResults: function (data, params) { |
195
|
|
|
params.page = params.page || 1; |
196
|
|
|
|
197
|
|
|
return { |
198
|
|
|
results: $.map(data.data, function (d) { |
199
|
|
|
d.id = d.$idField; |
200
|
|
|
d.text = d.$textField; |
201
|
|
|
return d; |
202
|
|
|
}), |
203
|
|
|
pagination: { |
204
|
|
|
more: data.next_page_url |
205
|
|
|
} |
206
|
|
|
}; |
207
|
|
|
}, |
208
|
|
|
cache: true |
209
|
|
|
}, |
210
|
|
|
minimumInputLength: 1, |
211
|
|
|
escapeMarkup: function (markup) { |
212
|
|
|
return markup; |
213
|
|
|
} |
214
|
|
|
}); |
215
|
|
|
|
216
|
|
|
EOT; |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: