SelectTable.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. protected static $js = [
  11. '@select-table',
  12. ];
  13. /**
  14. * @var DialogTable
  15. */
  16. protected $dialog;
  17. protected $style = 'primary';
  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. * @param array $options
  65. *
  66. * @return $this
  67. */
  68. public function options($options = [])
  69. {
  70. $this->options = $options;
  71. return $this;
  72. }
  73. /**
  74. * 设置选中数据显示.
  75. *
  76. * @param string $model
  77. * @param string $id
  78. * @param string $text
  79. *
  80. * @return $this
  81. */
  82. public function model(string $model, string $id = 'id', string $text = 'title')
  83. {
  84. return $this->options(function ($v) use ($model, $id, $text) {
  85. if (! $v) {
  86. return [];
  87. }
  88. return $model::find($v)->pluck($text, $id);
  89. });
  90. }
  91. protected function formatOptions()
  92. {
  93. $value = Helper::array($this->value());
  94. if ($this->options instanceof \Closure) {
  95. $this->options = $this->options->call($this->values(), $value, $this);
  96. }
  97. $values = [];
  98. foreach (Helper::array($this->options) as $id => $label) {
  99. foreach ($value as $v) {
  100. if ($v == $id && $v !== null) {
  101. $values[] = ['id' => $v, 'label' => $label];
  102. }
  103. }
  104. }
  105. $this->options = json_encode($values);
  106. }
  107. protected function addScript()
  108. {
  109. $this->script .= <<<JS
  110. Dcat.grid.SelectTable({
  111. dialog: replaceNestedFormIndex('#{$this->dialog->id()}'),
  112. container: replaceNestedFormIndex('#{$this->getAttribute('id')}'),
  113. input: replaceNestedFormIndex('#hidden-{$this->id}'),
  114. values: {$this->options},
  115. });
  116. JS;
  117. }
  118. protected function setUpTable()
  119. {
  120. $this->dialog
  121. ->id($this->getElementId())
  122. ->runScript(false)
  123. ->footer($this->renderFooter())
  124. ->button($this->renderButton());
  125. }
  126. public function render()
  127. {
  128. $this->setUpTable();
  129. $this->formatOptions();
  130. $name = $this->getElementName();
  131. $this->prepend('<i class="feather icon-arrow-up"></i>')
  132. ->defaultAttribute('class', 'form-control '.$this->getElementClassString())
  133. ->defaultAttribute('type', 'text')
  134. ->defaultAttribute('name', $name)
  135. ->defaultAttribute('id', 'container-'.$this->getElementId());
  136. $this->addVariables([
  137. 'prepend' => $this->prepend,
  138. 'append' => $this->append,
  139. 'style' => $this->style,
  140. 'dialog' => $this->dialog->render(),
  141. 'placeholder' => $this->placeholder(),
  142. ]);
  143. $this->script = $this->dialog->getScript();
  144. $this->addScript();
  145. return parent::render();
  146. }
  147. protected function renderButton()
  148. {
  149. return <<<HTML
  150. <div class="btn btn-{$this->style}">
  151. &nbsp;<i class="feather icon-arrow-up"></i>&nbsp;
  152. </div>
  153. HTML;
  154. }
  155. /**
  156. * 弹窗底部内容构建.
  157. *
  158. * @return string
  159. */
  160. protected function renderFooter()
  161. {
  162. $submit = trans('admin.submit');
  163. $cancel = trans('admin.cancel');
  164. return <<<HTML
  165. <button class="btn btn-primary btn-sm submit-btn" style="color: #fff">&nbsp;{$submit}&nbsp;</button>&nbsp;
  166. <button class="btn btn-white btn-sm cancel-btn">&nbsp;{$cancel}&nbsp;</button>
  167. HTML;
  168. }
  169. }