Select.php 9.1 KB

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