SelectTable.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Form\Field;
  4. use Dcat\Admin\Grid\LazyRenderable;
  5. use Dcat\Admin\Support\Helper;
  6. use Dcat\Admin\Widgets\DialogTable;
  7. use Illuminate\Support\Str;
  8. class SelectTable extends Field
  9. {
  10. use PlainInput;
  11. /**
  12. * @var DialogTable
  13. */
  14. protected $dialog;
  15. protected $style = 'primary';
  16. protected $visibleColumn;
  17. protected $key;
  18. public function __construct($column, $arguments = [])
  19. {
  20. parent::__construct($column, $arguments);
  21. $this->dialog = DialogTable::make($this->label);
  22. }
  23. /**
  24. * 设置弹窗标题.
  25. *
  26. * @param string $title
  27. *
  28. * @return $this
  29. */
  30. public function title($title)
  31. {
  32. $this->dialog->title($title);
  33. return $this;
  34. }
  35. /**
  36. * 设置弹窗宽度.
  37. *
  38. * @example
  39. * $this->width('500px');
  40. * $this->width('50%');
  41. *
  42. * @param string $width
  43. *
  44. * @return $this
  45. */
  46. public function dialogWidth(string $width)
  47. {
  48. $this->dialog->width($width);
  49. return $this;
  50. }
  51. /**
  52. * 设置表格异步渲染实例.
  53. *
  54. * @param LazyRenderable $renderable
  55. *
  56. * @return $this
  57. */
  58. public function from(LazyRenderable $renderable)
  59. {
  60. $this->dialog->from($renderable);
  61. return $this;
  62. }
  63. /**
  64. * 设置选中的key以及标题字段.
  65. *
  66. * @param $visibleColumn
  67. * @param $key
  68. *
  69. * @return $this
  70. */
  71. public function pluck(?string $visibleColumn, ?string $key = 'id')
  72. {
  73. $this->visibleColumn = $visibleColumn;
  74. $this->key = $key;
  75. return $this;
  76. }
  77. /**
  78. * 联动加载.
  79. *
  80. * @param string $field
  81. * @param string $sourceUrl
  82. * @param string $idField
  83. * @param string $textField
  84. *
  85. * @return $this
  86. */
  87. public function load($field, $sourceUrl, string $idField = 'id', string $textField = 'text')
  88. {
  89. if (Str::contains($field, '.')) {
  90. $field = $this->formatName($field);
  91. }
  92. $class = $this->normalizeElementClass($field);
  93. $url = admin_url($sourceUrl);
  94. return $this->addVariables(['load' => compact('url', 'class', 'idField', 'textField')]);
  95. }
  96. /**
  97. * 联动加载多个字段.
  98. *
  99. * @param string $fields
  100. * @param string $sourceUrls
  101. * @param string $idField
  102. * @param string $textField
  103. *
  104. * @return $this
  105. */
  106. public function loads($fields = [], $sourceUrls = [], string $idField = 'id', string $textField = 'text')
  107. {
  108. $fieldsStr = implode('^', array_map(function ($field) {
  109. if (Str::contains($field, '.')) {
  110. return $this->normalizeElementClass($field).'_';
  111. }
  112. return $this->normalizeElementClass($field);
  113. }, (array) $fields));
  114. $urlsStr = implode('^', array_map(function ($url) {
  115. return admin_url($url);
  116. }, (array) $sourceUrls));
  117. return $this->addVariables(['loads' => [
  118. 'fields' => $fieldsStr,
  119. 'urls' => $urlsStr,
  120. 'idField' => $idField,
  121. 'textField' => $textField,
  122. ]]);
  123. }
  124. /**
  125. * @param array $options
  126. *
  127. * @return $this
  128. */
  129. public function options($options = [])
  130. {
  131. $this->options = $options;
  132. return $this;
  133. }
  134. /**
  135. * 设置选中数据显示.
  136. *
  137. * @param string $model
  138. * @param string $id
  139. * @param string $text
  140. *
  141. * @return $this
  142. */
  143. public function model(string $model, string $id = 'id', string $text = 'title')
  144. {
  145. return $this->pluck($text, $id)->options(function ($v) use ($model, $id, $text) {
  146. if (! $v) {
  147. return [];
  148. }
  149. return $model::find($v)->pluck($text, $id);
  150. });
  151. }
  152. protected function formatOptions()
  153. {
  154. $value = Helper::array($this->value());
  155. if ($this->options instanceof \Closure) {
  156. $this->options = $this->options->call($this->values(), $value, $this);
  157. }
  158. $values = [];
  159. foreach (Helper::array($this->options) as $id => $label) {
  160. foreach ($value as $v) {
  161. if ($v == $id && $v !== null) {
  162. $values[] = ['id' => $v, 'label' => $label];
  163. }
  164. }
  165. }
  166. $this->options = $values;
  167. }
  168. protected function setUpTable()
  169. {
  170. $this->dialog
  171. ->footer($this->renderFooter())
  172. ->button($this->renderButton());
  173. // 设置选中的字段和待显示的标题字段
  174. $this->dialog
  175. ->getTable()
  176. ->getRenderable()
  177. ->payload([
  178. LazyRenderable::ROW_SELECTOR_COLUMN_NAME => [$this->key, $this->visibleColumn],
  179. ]);
  180. }
  181. public function render()
  182. {
  183. $this->setUpTable();
  184. $this->formatOptions();
  185. $this->prepend('<i class="feather icon-arrow-up"></i>')
  186. ->defaultAttribute('class', 'form-control '.$this->getElementClassString())
  187. ->defaultAttribute('type', 'text')
  188. ->defaultAttribute('name', $this->getElementName());
  189. $this->addVariables([
  190. 'prepend' => $this->prepend,
  191. 'append' => $this->append,
  192. 'style' => $this->style,
  193. 'dialog' => $this->dialog->render(),
  194. 'placeholder' => $this->placeholder(),
  195. 'dialogSelector' => $this->dialog->getElementSelector(),
  196. ]);
  197. return parent::render();
  198. }
  199. protected function renderButton()
  200. {
  201. return <<<HTML
  202. <div class="btn btn-{$this->style}">
  203. &nbsp;<i class="feather icon-arrow-up"></i>&nbsp;
  204. </div>
  205. HTML;
  206. }
  207. /**
  208. * 弹窗底部内容构建.
  209. *
  210. * @return string
  211. */
  212. protected function renderFooter()
  213. {
  214. $submit = trans('admin.submit');
  215. $cancel = trans('admin.cancel');
  216. return <<<HTML
  217. <button class="btn btn-primary btn-sm submit-btn" style="color: #fff">&nbsp;{$submit}&nbsp;</button>&nbsp;
  218. <button class="btn btn-white btn-sm cancel-btn">&nbsp;{$cancel}&nbsp;</button>
  219. HTML;
  220. }
  221. }