@@ 196-243 (lines=48) @@ | ||
193 | * @param $idField |
|
194 | * @param $textField |
|
195 | */ |
|
196 | public function ajax($resourceUrl, $idField = 'id', $textField = 'text') |
|
197 | { |
|
198 | $configs = array_merge([ |
|
199 | 'allowClear' => true, |
|
200 | 'placeholder' => trans('admin.choose'), |
|
201 | 'minimumInputLength' => 1, |
|
202 | ], $this->config); |
|
203 | ||
204 | $configs = json_encode($configs); |
|
205 | $configs = substr($configs, 1, strlen($configs) - 2); |
|
206 | ||
207 | $this->script = <<<EOT |
|
208 | ||
209 | $(".{$this->getElementClass()}").select2({ |
|
210 | ajax: { |
|
211 | url: "$resourceUrl", |
|
212 | dataType: 'json', |
|
213 | delay: 250, |
|
214 | data: function (params) { |
|
215 | return { |
|
216 | q: params.term, |
|
217 | page: params.page |
|
218 | }; |
|
219 | }, |
|
220 | processResults: function (data, params) { |
|
221 | params.page = params.page || 1; |
|
222 | ||
223 | return { |
|
224 | results: $.map(data.data, function (d) { |
|
225 | d.id = d.$idField; |
|
226 | d.text = d.$textField; |
|
227 | return d; |
|
228 | }), |
|
229 | pagination: { |
|
230 | more: data.next_page_url |
|
231 | } |
|
232 | }; |
|
233 | }, |
|
234 | cache: true |
|
235 | }, |
|
236 | $configs, |
|
237 | escapeMarkup: function (markup) { |
|
238 | return markup; |
|
239 | } |
|
240 | }); |
|
241 | ||
242 | EOT; |
|
243 | } |
|
244 | ||
245 | /** |
|
246 | * @return array |
@@ 307-356 (lines=50) @@ | ||
304 | * |
|
305 | * @return $this |
|
306 | */ |
|
307 | public function ajax($url, $idField = 'id', $textField = 'text') |
|
308 | { |
|
309 | $configs = array_merge([ |
|
310 | 'allowClear' => true, |
|
311 | 'placeholder' => $this->label, |
|
312 | 'minimumInputLength' => 1, |
|
313 | ], $this->config); |
|
314 | $configs = json_encode($configs); |
|
315 | $configs = substr($configs, 1, strlen($configs) - 2); |
|
316 | $this->script = <<<EOT |
|
317 | $("{$this->getElementClassSelector()}").select2({ |
|
318 | ajax: { |
|
319 | url: "$url", |
|
320 | dataType: 'json', |
|
321 | delay: 250, |
|
322 | data: function (params) { |
|
323 | return { |
|
324 | q: params.term, |
|
325 | page: params.page |
|
326 | }; |
|
327 | }, |
|
328 | processResults: function (data, params) { |
|
329 | params.page = params.page || 1; |
|
330 | return { |
|
331 | results: $.map(data.data, function (d) { |
|
332 | d.id = d.$idField; |
|
333 | d.text = d.$textField; |
|
334 | return d; |
|
335 | }), |
|
336 | pagination: { |
|
337 | more: data.next_page_url |
|
338 | } |
|
339 | }; |
|
340 | }, |
|
341 | cache: true |
|
342 | }, |
|
343 | $configs, |
|
344 | escapeMarkup: function (markup) { |
|
345 | return markup; |
|
346 | } |
|
347 | }); |
|
348 | EOT; |
|
349 | ||
350 | return $this; |
|
351 | } |
|
352 | ||
353 | /** |
|
354 | * Set config for select2. |
|
355 | * |
|
356 | * all configurations see https://select2.org/configuration/options-api |
|
357 | * |
|
358 | * @param string $key |
|
359 | * @param mixed $val |