Select.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form\Field;
  5. use Dcat\Admin\Support\Helper;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Str;
  9. class Select extends Field
  10. {
  11. /**
  12. * @var array
  13. */
  14. protected $groups = [];
  15. /**
  16. * @var array
  17. */
  18. protected $config = [];
  19. /**
  20. * Set options.
  21. *
  22. * @param array|\Closure|string $options
  23. *
  24. * @return $this|mixed
  25. */
  26. public function options($options = [])
  27. {
  28. if ($options instanceof \Closure) {
  29. $this->options = $options;
  30. return $this;
  31. }
  32. // remote options
  33. if (is_string($options)) {
  34. // reload selected
  35. if (class_exists($options) && in_array(Model::class, class_parents($options))) {
  36. return $this->model(...func_get_args());
  37. }
  38. return $this->loadRemoteOptions(...func_get_args());
  39. }
  40. $this->options = Helper::array($options);
  41. return $this;
  42. }
  43. /**
  44. * @param array $groups
  45. */
  46. /**
  47. * Set option groups.
  48. *
  49. * eg: $group = [
  50. * [
  51. * 'label' => 'xxxx',
  52. * 'options' => [
  53. * 1 => 'foo',
  54. * 2 => 'bar',
  55. * ...
  56. * ],
  57. * ...
  58. * ]
  59. *
  60. * @param array $groups
  61. *
  62. * @return $this
  63. */
  64. public function groups(array $groups)
  65. {
  66. $this->groups = $groups;
  67. return $this;
  68. }
  69. /**
  70. * Load options for other select on change.
  71. *
  72. * @param string $field
  73. * @param string $sourceUrl
  74. * @param string $idField
  75. * @param string $textField
  76. *
  77. * @return $this
  78. */
  79. public function load($field, $sourceUrl, $idField = 'id', $textField = 'text')
  80. {
  81. if (Str::contains($field, '.')) {
  82. $field = $this->formatName($field);
  83. $class = str_replace(['[', ']'], '_', $field);
  84. } else {
  85. $class = $field;
  86. }
  87. $script = <<<JS
  88. $(document).off('change', "{$this->getElementClassSelector()}");
  89. $(document).on('change', "{$this->getElementClassSelector()}", function () {
  90. var target = $(this).closest('.fields-group').find(".$class");
  91. $.get("$sourceUrl?q="+this.value, function (data) {
  92. target.find("option").remove();
  93. $(target).select2({
  94. data: $.map(data, function (d) {
  95. d.id = d.$idField;
  96. d.text = d.$textField;
  97. return d;
  98. })
  99. }).val(target.attr('data-value')).trigger('change');
  100. });
  101. });
  102. JS;
  103. Admin::script($script);
  104. return $this;
  105. }
  106. /**
  107. * Load options for other selects on change.
  108. *
  109. * @param string $fields
  110. * @param string $sourceUrls
  111. * @param string $idField
  112. * @param string $textField
  113. *
  114. * @return $this
  115. */
  116. public function loads($fields = [], $sourceUrls = [], $idField = 'id', $textField = 'text')
  117. {
  118. $fieldsStr = implode('.', $fields);
  119. $urlsStr = implode('^', $sourceUrls);
  120. $script = <<<JS
  121. var fields = '$fieldsStr'.split('.');
  122. var urls = '$urlsStr'.split('^');
  123. var refreshOptions = function(url, target) {
  124. $.get(url).then(function(data) {
  125. target.find("option").remove();
  126. $(target).select2({
  127. data: $.map(data, function (d) {
  128. d.id = d.$idField;
  129. d.text = d.$textField;
  130. return d;
  131. })
  132. }).trigger('change');
  133. });
  134. };
  135. $(document).off('change', "{$this->getElementClassSelector()}");
  136. $(document).on('change', "{$this->getElementClassSelector()}", function () {
  137. var _this = this;
  138. var promises = [];
  139. fields.forEach(function(field, index){
  140. var target = $(_this).closest('.fields-group').find('.' + fields[index]);
  141. promises.push(refreshOptions(urls[index] + "?q="+ _this.value, target));
  142. });
  143. $.when(promises).then(function() {
  144. console.log('开始更新其它select的选择options');
  145. });
  146. });
  147. JS;
  148. Admin::script($script);
  149. return $this;
  150. }
  151. /**
  152. * Load options from current selected resource(s).
  153. *
  154. * @param string $model
  155. * @param string $idField
  156. * @param string $textField
  157. *
  158. * @return $this
  159. */
  160. public function model($model, $idField = 'id', $textField = 'name')
  161. {
  162. if (! class_exists($model)
  163. || ! in_array(Model::class, class_parents($model))
  164. ) {
  165. throw new \InvalidArgumentException("[$model] must be a valid model class");
  166. }
  167. $this->options = function ($value) use ($model, $idField, $textField) {
  168. if (empty($value)) {
  169. return [];
  170. }
  171. $resources = [];
  172. if (is_array($value)) {
  173. if (Arr::isAssoc($value)) {
  174. $resources[] = Arr::get($value, $idField);
  175. } else {
  176. $resources = array_column($value, $idField);
  177. }
  178. } else {
  179. $resources[] = $value;
  180. }
  181. return $model::find($resources)->pluck($textField, $idField)->toArray();
  182. };
  183. return $this;
  184. }
  185. /**
  186. * Load options from remote.
  187. *
  188. * @param string $url
  189. * @param array $parameters
  190. * @param array $options
  191. *
  192. * @return $this
  193. */
  194. protected function loadRemoteOptions($url, $parameters = [], $options = [])
  195. {
  196. $ajaxOptions = [
  197. 'url' => $url.'?'.http_build_query($parameters),
  198. ];
  199. $configs = array_merge([
  200. 'allowClear' => true,
  201. 'placeholder' => [
  202. 'id' => '',
  203. 'text' => trans('admin.choose'),
  204. ],
  205. ], $this->config);
  206. $configs = json_encode($configs);
  207. $configs = substr($configs, 1, strlen($configs) - 2);
  208. $ajaxOptions = json_encode(array_merge($ajaxOptions, $options));
  209. $this->script = <<<JS
  210. $.ajax($ajaxOptions).done(function(data) {
  211. var select = $("{$this->getElementClassSelector()}");
  212. select.select2({
  213. data: data,
  214. $configs
  215. });
  216. var value = select.data('value') + '';
  217. if (value) {
  218. value = value.split(',');
  219. select.select2('val', value);
  220. }
  221. });
  222. JS;
  223. return $this;
  224. }
  225. /**
  226. * Load options from ajax results.
  227. *
  228. * @param string $url
  229. * @param $idField
  230. * @param $textField
  231. *
  232. * @return $this
  233. */
  234. public function ajax($url, $idField = 'id', $textField = 'text')
  235. {
  236. $configs = array_merge([
  237. 'allowClear' => true,
  238. 'placeholder' => $this->label,
  239. 'minimumInputLength' => 1,
  240. ], $this->config);
  241. $configs = json_encode($configs);
  242. $configs = substr($configs, 1, strlen($configs) - 2);
  243. $this->script = <<<JS
  244. $("{$this->getElementClassSelector()}").select2({
  245. ajax: {
  246. url: "$url",
  247. dataType: 'json',
  248. delay: 250,
  249. data: function (params) {
  250. return {
  251. q: params.term,
  252. page: params.page
  253. };
  254. },
  255. processResults: function (data, params) {
  256. params.page = params.page || 1;
  257. return {
  258. results: $.map(data.data, function (d) {
  259. d.id = d.$idField;
  260. d.text = d.$textField;
  261. return d;
  262. }),
  263. pagination: {
  264. more: data.next_page_url
  265. }
  266. };
  267. },
  268. cache: true
  269. },
  270. $configs,
  271. escapeMarkup: function (markup) {
  272. return markup;
  273. }
  274. });
  275. JS;
  276. return $this;
  277. }
  278. /**
  279. * Set config for select2.
  280. *
  281. * all configurations see https://select2.org/configuration/options-api
  282. *
  283. * @param string $key
  284. * @param mixed $val
  285. *
  286. * @return $this
  287. */
  288. public function config($key, $val)
  289. {
  290. $this->config[$key] = $val;
  291. return $this;
  292. }
  293. /**
  294. * Disable clear button.
  295. *
  296. * @return $this
  297. */
  298. public function disableClearButton()
  299. {
  300. return $this->config('allowClear', false);
  301. }
  302. /**
  303. * {@inheritdoc}
  304. */
  305. public function render()
  306. {
  307. $configs = array_merge([
  308. 'allowClear' => true,
  309. 'placeholder' => [
  310. 'id' => '',
  311. 'text' => $this->label,
  312. ],
  313. ], $this->config);
  314. $configs = json_encode($configs);
  315. if (empty($this->script)) {
  316. $this->script = "$(\"{$this->getElementClassSelector()}\").select2($configs);";
  317. }
  318. if ($this->options instanceof \Closure) {
  319. $this->options = $this->options->bindTo($this->getFormModel());
  320. $this->options(call_user_func($this->options, $this->value(), $this));
  321. }
  322. $this->options = array_filter($this->options, 'strlen');
  323. $this->addVariables([
  324. 'options' => $this->options,
  325. 'groups' => $this->groups,
  326. ]);
  327. $this->attribute('data-value', implode(',', Helper::array($this->value())));
  328. return parent::render();
  329. }
  330. public static function collectAssets()
  331. {
  332. Admin::collectComponentAssets('select2');
  333. }
  334. }