Cascader.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace common\widgets\cascader;
  3. use yii\helpers\Json;
  4. use yii\helpers\Html;
  5. use yii\widgets\InputWidget;
  6. use common\helpers\StringHelper;
  7. use common\helpers\ArrayHelper;
  8. /**
  9. *
  10. *
  11. * Class Cascader
  12. * @package common\widgets\area
  13. */
  14. class Cascader extends InputWidget
  15. {
  16. /**
  17. * 默认数据
  18. *
  19. * @var array
  20. *
  21. * [
  22. * [
  23. * 'id' = '1',
  24. * 'title' = 'demo',
  25. * 'pid' = '0',
  26. * ]
  27. * ]
  28. */
  29. public $data = [];
  30. /**
  31. * 默认重组 pid 值
  32. *
  33. * @var int
  34. */
  35. public $pid = 0;
  36. /**
  37. * 多选
  38. *
  39. * @var bool
  40. */
  41. public $multiple = false;
  42. /**
  43. * 开启选择任意级
  44. *
  45. * @var bool
  46. */
  47. public $changeOnSelect = false;
  48. /**
  49. * 动态加载
  50. *
  51. * @var bool
  52. */
  53. public $dynamicLoading = false;
  54. /**
  55. * @var array
  56. */
  57. public $options = [];
  58. /**
  59. * 盒子ID
  60. *
  61. * @var
  62. */
  63. protected $boxId;
  64. /**
  65. * @var array
  66. */
  67. protected $items = [];
  68. /**
  69. * @throws \yii\base\InvalidConfigException
  70. */
  71. public function init()
  72. {
  73. parent::init();
  74. foreach ($this->data as $datum) {
  75. $this->items[] = [
  76. 'value' => trim($datum['id']),
  77. 'pid' => trim($datum['pid']),
  78. 'label' => trim($datum['title']),
  79. ];
  80. }
  81. $this->items = ArrayHelper::itemsMerge($this->items, $this->pid, 'value', 'pid', 'children');
  82. $this->options = ArrayHelper::merge([
  83. 'style' => 'width:420px',
  84. 'placeholder' => '搜索或点击下拉选择'
  85. ], $this->options);
  86. $name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->name;
  87. $this->boxId = md5($name) . StringHelper::uuid('uniqid');
  88. }
  89. /**
  90. * @return string
  91. */
  92. public function run()
  93. {
  94. $value = $this->hasModel() ? Html::getAttributeValue($this->model, $this->attribute) : $this->value;
  95. $name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->name;
  96. $selected = [];
  97. if ($this->multiple == true) {
  98. $name = $name . '[]';
  99. empty($value) && $value = [];
  100. foreach ($value as $item) {
  101. $parents = ArrayHelper::getParents($this->data, $item);
  102. !empty($parents) && $selected[] = [
  103. 'parents' => $parents,
  104. 'id' => $item
  105. ];
  106. }
  107. } else {
  108. is_array($value) && $value = $value[0];
  109. $parents = ArrayHelper::getParents($this->data, $value);
  110. !empty($parents) && $selected[] = [
  111. 'parents' => $parents,
  112. 'id' => $value
  113. ];
  114. }
  115. empty($selected) && $selected[] = [
  116. 'parents' => [],
  117. 'id' => 0
  118. ];
  119. $this->registerClientScript();
  120. return $this->render('cascader', [
  121. 'value' => $value,
  122. 'name' => $name,
  123. 'selected' => $selected,
  124. 'items' => Json::encode($this->items),
  125. 'boxId' => $this->boxId,
  126. 'multiple' => $this->multiple,
  127. 'options' => $this->options,
  128. 'changeOnSelect' => $this->changeOnSelect,
  129. 'dynamicLoading' => $this->dynamicLoading,
  130. ]);
  131. }
  132. /**
  133. * 注册资源
  134. */
  135. protected function registerClientScript()
  136. {
  137. $view = $this->getView();
  138. $view->registerJs(<<<Js
  139. $(document).on("click", ".cascader-input-plus", function (e) {
  140. var boxId = $(this).parent().parent().parent().data('id');
  141. var random = getRandomString(20);
  142. var html = template('cascaderBoxHtml-' + boxId, {boxId: random});
  143. $(this).parent().parent().parent().parent().append(html);
  144. cascaderboxInit(random);
  145. });
  146. Js
  147. );
  148. }
  149. }
粤ICP备19079148号