SelectTable.php 4.6 KB

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