SelectTable.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. public function __construct($column, $arguments = [])
  16. {
  17. parent::__construct($column, $arguments);
  18. $this->dialog = DialogTable::make($this->label);
  19. }
  20. /**
  21. * 设置弹窗标题.
  22. *
  23. * @param string $title
  24. *
  25. * @return $this
  26. */
  27. public function title($title)
  28. {
  29. $this->dialog->title($title);
  30. return $this;
  31. }
  32. /**
  33. * 设置弹窗宽度.
  34. *
  35. * @example
  36. * $this->width('500px');
  37. * $this->width('50%');
  38. *
  39. * @param string $width
  40. *
  41. * @return $this
  42. */
  43. public function dialogWidth(string $width)
  44. {
  45. $this->dialog->width($width);
  46. return $this;
  47. }
  48. /**
  49. * 设置表格异步渲染实例.
  50. *
  51. * @param LazyRenderable $renderable
  52. *
  53. * @return $this
  54. */
  55. public function from(LazyRenderable $renderable)
  56. {
  57. $this->dialog->from($renderable);
  58. return $this;
  59. }
  60. /**
  61. * @param array $options
  62. *
  63. * @return $this
  64. */
  65. public function options($options = [])
  66. {
  67. $this->options = $options;
  68. return $this;
  69. }
  70. /**
  71. * 设置选中数据显示.
  72. *
  73. * @param string $model
  74. * @param string $id
  75. * @param string $text
  76. *
  77. * @return $this
  78. */
  79. public function model(string $model, string $id = 'id', string $text = 'title')
  80. {
  81. return $this->options(function ($v) use ($model, $id, $text) {
  82. if (! $v) {
  83. return [];
  84. }
  85. return $model::find($v)->pluck($text, $id);
  86. });
  87. }
  88. protected function formatOptions()
  89. {
  90. $value = Helper::array($this->value());
  91. if ($this->options instanceof \Closure) {
  92. $this->options = $this->options->call($this->values(), $value, $this);
  93. }
  94. $values = [];
  95. foreach (Helper::array($this->options) as $id => $label) {
  96. foreach ($value as $v) {
  97. if ($v == $id && $v !== null) {
  98. $values[] = ['id' => $v, 'label' => $label];
  99. }
  100. }
  101. }
  102. $this->options = json_encode($values);
  103. }
  104. protected function setUpTable()
  105. {
  106. $this->dialog
  107. ->id($this->getElementId())
  108. ->runScript(false)
  109. ->footer($this->renderFooter())
  110. ->button($this->renderButton());
  111. }
  112. public function render()
  113. {
  114. $this->setUpTable();
  115. $this->formatOptions();
  116. $this->prepend('<i class="feather icon-arrow-up"></i>')
  117. ->defaultAttribute('class', 'form-control '.$this->getElementClassString())
  118. ->defaultAttribute('type', 'text')
  119. ->defaultAttribute('name', $this->getElementName())
  120. ->defaultAttribute('id', 'container-'.$this->getElementId());
  121. $this->addVariables([
  122. 'prepend' => $this->prepend,
  123. 'append' => $this->append,
  124. 'style' => $this->style,
  125. 'dialog' => $this->dialog->render(),
  126. 'placeholder' => $this->placeholder(),
  127. 'dialogId' => $this->dialog->id(),
  128. 'dialogScript' => $this->dialog->getScript(),
  129. ]);
  130. return parent::render();
  131. }
  132. protected function renderButton()
  133. {
  134. return <<<HTML
  135. <div class="btn btn-{$this->style}">
  136. &nbsp;<i class="feather icon-arrow-up"></i>&nbsp;
  137. </div>
  138. HTML;
  139. }
  140. /**
  141. * 弹窗底部内容构建.
  142. *
  143. * @return string
  144. */
  145. protected function renderFooter()
  146. {
  147. $submit = trans('admin.submit');
  148. $cancel = trans('admin.cancel');
  149. return <<<HTML
  150. <button class="btn btn-primary btn-sm submit-btn" style="color: #fff">&nbsp;{$submit}&nbsp;</button>&nbsp;
  151. <button class="btn btn-white btn-sm cancel-btn">&nbsp;{$cancel}&nbsp;</button>
  152. HTML;
  153. }
  154. }