Tree.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * PHP表单生成器
  4. *
  5. * @package FormBuilder
  6. * @author xaboy <xaboy2005@qq.com>
  7. * @version 2.0
  8. * @license MIT
  9. * @link https://github.com/xaboy/form-builder
  10. */
  11. namespace FormBuilder\UI\Iview\Components;
  12. use FormBuilder\Driver\FormComponent;
  13. use FormBuilder\Factory\Iview;
  14. /**
  15. * 树型组件
  16. * Class Tree
  17. *
  18. * @method $this type(string $type) 类型,可选值为 checked、selected
  19. * @method $this multiple(bool $bool) 是否支持多选, 当`type=selected`并且`multiple=false`, 默认为false, 值为string或Number类型,其他情况为Array类型
  20. * @method $this showCheckbox(bool $bool) 是否显示多选框, 默认为false
  21. * @method $this emptyText(string $emptyText) 没有数据时的提示, 默认为'暂无数据'
  22. */
  23. class Tree extends FormComponent
  24. {
  25. /**
  26. * 选中
  27. */
  28. const TYPE_SELECTED = 'selected';
  29. /**
  30. * 选择
  31. */
  32. const TYPE_CHECKED = 'checked';
  33. protected $selectComponent = true;
  34. protected $defaultProps = [
  35. 'type' => self::TYPE_CHECKED,
  36. 'data' => [],
  37. 'multiple' => true
  38. ];
  39. protected static $propsRule = [
  40. 'type' => 'string',
  41. 'multiple' => 'bool',
  42. 'showCheckbox' => 'bool',
  43. 'emptyText' => 'string',
  44. ];
  45. /**
  46. * @param array $treeData
  47. * @return $this
  48. */
  49. public function data(array $treeData)
  50. {
  51. $this->props['data'] = [];
  52. foreach ($treeData as $child) {
  53. $this->props['data'][] = $child instanceof TreeData
  54. ? $child->getOption()
  55. : $child;
  56. }
  57. return $this;
  58. }
  59. /**
  60. * @param string $var
  61. * @return $this
  62. */
  63. public function jsData($var)
  64. {
  65. $this->props['data'] = 'js.' . (string)$var;
  66. return $this;
  67. }
  68. public function createValidate()
  69. {
  70. if ($this->props['multiple'])
  71. return Iview::validateArr();
  72. else
  73. return Iview::validateStr();
  74. }
  75. }