Select.php 9.3 KB

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