SelectTable.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. class SelectTable extends Field
  8. {
  9. use PlainInput;
  10. use CanLoadFields;
  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. * @param array $options
  79. *
  80. * @return $this
  81. */
  82. public function options($options = [])
  83. {
  84. $this->options = $options;
  85. return $this;
  86. }
  87. /**
  88. * 设置选中数据显示.
  89. *
  90. * @param string $model
  91. * @param string $id
  92. * @param string $text
  93. *
  94. * @return $this
  95. */
  96. public function model(string $model, string $id = 'id', string $text = 'title')
  97. {
  98. return $this->pluck($text, $id)->options(function ($v) use ($model, $id, $text) {
  99. if (! $v) {
  100. return [];
  101. }
  102. return $model::find($v)->pluck($text, $id);
  103. });
  104. }
  105. protected function formatOptions()
  106. {
  107. $value = Helper::array($this->value());
  108. if ($this->options instanceof \Closure) {
  109. $this->options = $this->options->call($this->values(), $value, $this);
  110. }
  111. $values = [];
  112. foreach (Helper::array($this->options) as $id => $label) {
  113. foreach ($value as $v) {
  114. if ($v == $id && $v !== null) {
  115. $values[] = ['id' => $v, 'label' => $label];
  116. }
  117. }
  118. }
  119. $this->options = $values;
  120. }
  121. /**
  122. * @return string
  123. */
  124. protected function defaultPlaceholder()
  125. {
  126. return trans('admin.choose').' '.$this->label;
  127. }
  128. protected function setUpTable()
  129. {
  130. $this->dialog
  131. ->footer($this->renderFooter())
  132. ->button($this->renderButton());
  133. // 设置选中的字段和待显示的标题字段
  134. $this->dialog
  135. ->getTable()
  136. ->getRenderable()
  137. ->payload([
  138. LazyRenderable::ROW_SELECTOR_COLUMN_NAME => [$this->key, $this->visibleColumn],
  139. ]);
  140. }
  141. public function render()
  142. {
  143. $this->setUpTable();
  144. $this->formatOptions();
  145. $this->prepend('<i class="feather icon-arrow-up"></i>')
  146. ->defaultAttribute('class', 'form-control '.$this->getElementClassString())
  147. ->defaultAttribute('type', 'text')
  148. ->defaultAttribute('name', $this->getElementName());
  149. $this->addVariables([
  150. 'prepend' => $this->prepend,
  151. 'append' => $this->append,
  152. 'style' => $this->style,
  153. 'dialog' => $this->dialog->render(),
  154. 'placeholder' => $this->placeholder(),
  155. 'dialogSelector' => $this->dialog->getElementSelector(),
  156. ]);
  157. return parent::render();
  158. }
  159. protected function renderButton()
  160. {
  161. return <<<HTML
  162. <div class="btn btn-{$this->style}">
  163. &nbsp;<i class="feather icon-arrow-up"></i>&nbsp;
  164. </div>
  165. HTML;
  166. }
  167. /**
  168. * 弹窗底部内容构建.
  169. *
  170. * @return string
  171. */
  172. protected function renderFooter()
  173. {
  174. $submit = trans('admin.submit');
  175. $cancel = trans('admin.cancel');
  176. return <<<HTML
  177. <button class="btn btn-primary btn-sm submit-btn" style="color: #fff">&nbsp;{$submit}&nbsp;</button>&nbsp;
  178. <button class="btn btn-white btn-sm cancel-btn">&nbsp;{$cancel}&nbsp;</button>
  179. HTML;
  180. }
  181. }