SelectTable.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. * @return $this
  28. */
  29. public function title($title)
  30. {
  31. $this->dialog->title($title);
  32. return $this;
  33. }
  34. /**
  35. * 设置弹窗宽度.
  36. *
  37. * @example
  38. * $this->width('500px');
  39. * $this->width('50%');
  40. *
  41. * @param string $width
  42. * @return $this
  43. */
  44. public function dialogWidth(string $width)
  45. {
  46. $this->dialog->width($width);
  47. return $this;
  48. }
  49. /**
  50. * 设置表格异步渲染实例.
  51. *
  52. * @param LazyRenderable $renderable
  53. * @return $this
  54. */
  55. public function from(LazyRenderable $renderable)
  56. {
  57. $this->dialog->from($renderable);
  58. return $this;
  59. }
  60. /**
  61. * 设置选中的key以及标题字段.
  62. *
  63. * @param $visibleColumn
  64. * @param $key
  65. * @return $this
  66. */
  67. public function pluck(?string $visibleColumn, ?string $key = 'id')
  68. {
  69. $this->visibleColumn = $visibleColumn;
  70. $this->key = $key;
  71. return $this;
  72. }
  73. /**
  74. * @param array $options
  75. * @return $this
  76. */
  77. public function options($options = [])
  78. {
  79. $this->options = $options;
  80. return $this;
  81. }
  82. /**
  83. * 设置选中数据显示.
  84. *
  85. * @param string $model
  86. * @param string $id
  87. * @param string $text
  88. * @return $this
  89. */
  90. public function model(string $model, string $id = 'id', string $text = 'title')
  91. {
  92. return $this->pluck($text, $id)->options(function ($v) use ($model, $id, $text) {
  93. if (! $v) {
  94. return [];
  95. }
  96. return $model::whereIn($id, Helper::array($v))->pluck($text, $id);
  97. });
  98. }
  99. protected function formatOptions()
  100. {
  101. $value = Helper::array($this->value());
  102. if ($this->options instanceof \Closure) {
  103. $this->options = $this->options->call($this->values(), $value, $this);
  104. }
  105. $values = [];
  106. foreach (Helper::array($this->options) as $id => $label) {
  107. foreach ($value as $v) {
  108. if ($v == $id && $v !== null) {
  109. $values[] = ['id' => $v, 'label' => $label];
  110. }
  111. }
  112. }
  113. $this->options = $values;
  114. }
  115. /**
  116. * @return string
  117. */
  118. protected function defaultPlaceholder()
  119. {
  120. return trans('admin.choose').' '.$this->label;
  121. }
  122. protected function setUpTable()
  123. {
  124. $this->dialog
  125. ->footer($this->renderFooter())
  126. ->button($this->renderButton());
  127. // 设置选中的字段和待显示的标题字段
  128. $this->dialog
  129. ->getTable()
  130. ->getRenderable()
  131. ->payload([
  132. LazyRenderable::ROW_SELECTOR_COLUMN_NAME => [$this->key, $this->visibleColumn],
  133. ]);
  134. }
  135. public function render()
  136. {
  137. $this->setUpTable();
  138. $this->formatOptions();
  139. $this->prepend('<i class="feather icon-arrow-up"></i>')
  140. ->defaultAttribute('class', 'form-control '.$this->getElementClassString())
  141. ->defaultAttribute('type', 'text')
  142. ->defaultAttribute('name', $this->getElementName());
  143. $this->addVariables([
  144. 'prepend' => $this->prepend,
  145. 'append' => $this->append,
  146. 'style' => $this->style,
  147. 'dialog' => $this->dialog->render(),
  148. 'placeholder' => $this->placeholder(),
  149. 'dialogSelector' => $this->dialog->getElementSelector(),
  150. ]);
  151. return parent::render();
  152. }
  153. protected function renderButton()
  154. {
  155. return <<<HTML
  156. <div class="btn btn-{$this->style}">
  157. &nbsp;<i class="feather icon-arrow-up"></i>&nbsp;
  158. </div>
  159. HTML;
  160. }
  161. /**
  162. * 弹窗底部内容构建.
  163. *
  164. * @return string
  165. */
  166. protected function renderFooter()
  167. {
  168. $submit = trans('admin.submit');
  169. $cancel = trans('admin.cancel');
  170. return <<<HTML
  171. <button class="btn btn-primary btn-sm submit-btn" style="color: #fff">&nbsp;{$submit}&nbsp;</button>&nbsp;
  172. <button class="btn btn-white btn-sm cancel-btn">&nbsp;{$cancel}&nbsp;</button>
  173. HTML;
  174. }
  175. }