| @@ 283-332 (lines=50) @@ | ||
| 280 | * |
|
| 281 | * @return $this |
|
| 282 | */ |
|
| 283 | public function ajax($url, $idField = 'id', $textField = 'text') |
|
| 284 | { |
|
| 285 | $configs = array_merge([ |
|
| 286 | 'allowClear' => true, |
|
| 287 | 'placeholder' => $this->label, |
|
| 288 | 'minimumInputLength' => 1, |
|
| 289 | ], $this->config); |
|
| 290 | ||
| 291 | $configs = json_encode($configs); |
|
| 292 | $configs = substr($configs, 1, strlen($configs) - 2); |
|
| 293 | ||
| 294 | $this->script = <<<EOT |
|
| 295 | ||
| 296 | $("{$this->getElementClassSelector()}").select2({ |
|
| 297 | ajax: { |
|
| 298 | url: "$url", |
|
| 299 | dataType: 'json', |
|
| 300 | delay: 250, |
|
| 301 | data: function (params) { |
|
| 302 | return { |
|
| 303 | q: params.term, |
|
| 304 | page: params.page |
|
| 305 | }; |
|
| 306 | }, |
|
| 307 | processResults: function (data, params) { |
|
| 308 | params.page = params.page || 1; |
|
| 309 | ||
| 310 | return { |
|
| 311 | results: $.map(data.data, function (d) { |
|
| 312 | d.id = d.$idField; |
|
| 313 | d.text = d.$textField; |
|
| 314 | return d; |
|
| 315 | }), |
|
| 316 | pagination: { |
|
| 317 | more: data.next_page_url |
|
| 318 | } |
|
| 319 | }; |
|
| 320 | }, |
|
| 321 | cache: true |
|
| 322 | }, |
|
| 323 | $configs, |
|
| 324 | escapeMarkup: function (markup) { |
|
| 325 | return markup; |
|
| 326 | } |
|
| 327 | }); |
|
| 328 | ||
| 329 | EOT; |
|
| 330 | ||
| 331 | return $this; |
|
| 332 | } |
|
| 333 | ||
| 334 | /** |
|
| 335 | * Set config for select2. |
|
| @@ 168-215 (lines=48) @@ | ||
| 165 | * @param $idField |
|
| 166 | * @param $textField |
|
| 167 | */ |
|
| 168 | public function ajax($resourceUrl, $idField = 'id', $textField = 'text') |
|
| 169 | { |
|
| 170 | $configs = array_merge([ |
|
| 171 | 'allowClear' => true, |
|
| 172 | 'placeholder' => trans('admin.choose'), |
|
| 173 | 'minimumInputLength' => 1, |
|
| 174 | ], $this->config); |
|
| 175 | ||
| 176 | $configs = json_encode($configs); |
|
| 177 | $configs = substr($configs, 1, strlen($configs) - 2); |
|
| 178 | ||
| 179 | $this->script = <<<EOT |
|
| 180 | ||
| 181 | $(".{$this->getElementClass()}").select2({ |
|
| 182 | ajax: { |
|
| 183 | url: "$resourceUrl", |
|
| 184 | dataType: 'json', |
|
| 185 | delay: 250, |
|
| 186 | data: function (params) { |
|
| 187 | return { |
|
| 188 | q: params.term, |
|
| 189 | page: params.page |
|
| 190 | }; |
|
| 191 | }, |
|
| 192 | processResults: function (data, params) { |
|
| 193 | params.page = params.page || 1; |
|
| 194 | ||
| 195 | return { |
|
| 196 | results: $.map(data.data, function (d) { |
|
| 197 | d.id = d.$idField; |
|
| 198 | d.text = d.$textField; |
|
| 199 | return d; |
|
| 200 | }), |
|
| 201 | pagination: { |
|
| 202 | more: data.next_page_url |
|
| 203 | } |
|
| 204 | }; |
|
| 205 | }, |
|
| 206 | cache: true |
|
| 207 | }, |
|
| 208 | $configs, |
|
| 209 | escapeMarkup: function (markup) { |
|
| 210 | return markup; |
|
| 211 | } |
|
| 212 | }); |
|
| 213 | ||
| 214 | EOT; |
|
| 215 | } |
|
| 216 | ||
| 217 | /** |
|
| 218 | * @return array |
|