| @@ 292-341 (lines=50) @@ | ||
| 289 | * |
|
| 290 | * @return $this |
|
| 291 | */ |
|
| 292 | public function ajax($url, $idField = 'id', $textField = 'text') |
|
| 293 | { |
|
| 294 | $configs = array_merge([ |
|
| 295 | 'allowClear' => true, |
|
| 296 | 'placeholder' => $this->label, |
|
| 297 | 'minimumInputLength' => 1, |
|
| 298 | ], $this->config); |
|
| 299 | ||
| 300 | $configs = json_encode($configs); |
|
| 301 | $configs = substr($configs, 1, strlen($configs) - 2); |
|
| 302 | ||
| 303 | $this->script = <<<EOT |
|
| 304 | ||
| 305 | $("{$this->getElementClassSelector()}").select2({ |
|
| 306 | ajax: { |
|
| 307 | url: "$url", |
|
| 308 | dataType: 'json', |
|
| 309 | delay: 250, |
|
| 310 | data: function (params) { |
|
| 311 | return { |
|
| 312 | q: params.term, |
|
| 313 | page: params.page |
|
| 314 | }; |
|
| 315 | }, |
|
| 316 | processResults: function (data, params) { |
|
| 317 | params.page = params.page || 1; |
|
| 318 | ||
| 319 | return { |
|
| 320 | results: $.map(data.data, function (d) { |
|
| 321 | d.id = d.$idField; |
|
| 322 | d.text = d.$textField; |
|
| 323 | return d; |
|
| 324 | }), |
|
| 325 | pagination: { |
|
| 326 | more: data.next_page_url |
|
| 327 | } |
|
| 328 | }; |
|
| 329 | }, |
|
| 330 | cache: true |
|
| 331 | }, |
|
| 332 | $configs, |
|
| 333 | escapeMarkup: function (markup) { |
|
| 334 | return markup; |
|
| 335 | } |
|
| 336 | }); |
|
| 337 | ||
| 338 | EOT; |
|
| 339 | ||
| 340 | return $this; |
|
| 341 | } |
|
| 342 | ||
| 343 | /** |
|
| 344 | * Set config for select2. |
|
| @@ 193-240 (lines=48) @@ | ||
| 190 | * @param $idField |
|
| 191 | * @param $textField |
|
| 192 | */ |
|
| 193 | public function ajax($resourceUrl, $idField = 'id', $textField = 'text') |
|
| 194 | { |
|
| 195 | $configs = array_merge([ |
|
| 196 | 'allowClear' => true, |
|
| 197 | 'placeholder' => trans('admin.choose'), |
|
| 198 | 'minimumInputLength' => 1, |
|
| 199 | ], $this->config); |
|
| 200 | ||
| 201 | $configs = json_encode($configs); |
|
| 202 | $configs = substr($configs, 1, strlen($configs) - 2); |
|
| 203 | ||
| 204 | $this->script = <<<EOT |
|
| 205 | ||
| 206 | $(".{$this->getElementClass()}").select2({ |
|
| 207 | ajax: { |
|
| 208 | url: "$resourceUrl", |
|
| 209 | dataType: 'json', |
|
| 210 | delay: 250, |
|
| 211 | data: function (params) { |
|
| 212 | return { |
|
| 213 | q: params.term, |
|
| 214 | page: params.page |
|
| 215 | }; |
|
| 216 | }, |
|
| 217 | processResults: function (data, params) { |
|
| 218 | params.page = params.page || 1; |
|
| 219 | ||
| 220 | return { |
|
| 221 | results: $.map(data.data, function (d) { |
|
| 222 | d.id = d.$idField; |
|
| 223 | d.text = d.$textField; |
|
| 224 | return d; |
|
| 225 | }), |
|
| 226 | pagination: { |
|
| 227 | more: data.next_page_url |
|
| 228 | } |
|
| 229 | }; |
|
| 230 | }, |
|
| 231 | cache: true |
|
| 232 | }, |
|
| 233 | $configs, |
|
| 234 | escapeMarkup: function (markup) { |
|
| 235 | return markup; |
|
| 236 | } |
|
| 237 | }); |
|
| 238 | ||
| 239 | EOT; |
|
| 240 | } |
|
| 241 | ||
| 242 | /** |
|
| 243 | * @return array |
|