MultipleSelectTable.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Support\Helper;
  4. class MultipleSelectTable extends SelectTable
  5. {
  6. public static $css = [
  7. '@select2',
  8. ];
  9. protected $view = 'admin::form.selecttable';
  10. /**
  11. * @var int
  12. */
  13. protected $max = 0;
  14. /**
  15. * 设置最大选择数量.
  16. *
  17. * @param int $max
  18. *
  19. * @return $this
  20. */
  21. public function max(int $max)
  22. {
  23. $this->max = $max;
  24. return $this;
  25. }
  26. protected function addScript()
  27. {
  28. $this->script .= <<<JS
  29. Dcat.grid.SelectTable({
  30. dialog: replaceNestedFormIndex('#{$this->dialog->id()}'),
  31. container: replaceNestedFormIndex('#{$this->getAttribute('id')}'),
  32. input: replaceNestedFormIndex('#hidden-{$this->id}'),
  33. multiple: true,
  34. max: {$this->max},
  35. values: {$this->options},
  36. });
  37. JS;
  38. }
  39. /**
  40. * 转化为数组格式保存.
  41. *
  42. * @param mixed $value
  43. *
  44. * @return array|mixed
  45. */
  46. public function prepareInputValue($value)
  47. {
  48. return Helper::array($value, true);
  49. }
  50. }