data as $datum) { $this->items[] = [ 'value' => trim($datum['id']), 'pid' => trim($datum['pid']), 'label' => trim($datum['title']), ]; } $this->items = ArrayHelper::itemsMerge($this->items, $this->pid, 'value', 'pid', 'children'); $this->options = ArrayHelper::merge([ 'style' => 'width:420px', 'placeholder' => '搜索或点击下拉选择' ], $this->options); $name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->name; $this->boxId = md5($name) . StringHelper::uuid('uniqid'); } /** * @return string */ public function run() { $value = $this->hasModel() ? Html::getAttributeValue($this->model, $this->attribute) : $this->value; $name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->name; $selected = []; if ($this->multiple == true) { $name = $name . '[]'; empty($value) && $value = []; foreach ($value as $item) { $parents = ArrayHelper::getParents($this->data, $item); !empty($parents) && $selected[] = [ 'parents' => $parents, 'id' => $item ]; } } else { is_array($value) && $value = $value[0]; $parents = ArrayHelper::getParents($this->data, $value); !empty($parents) && $selected[] = [ 'parents' => $parents, 'id' => $value ]; } empty($selected) && $selected[] = [ 'parents' => [], 'id' => 0 ]; $this->registerClientScript(); return $this->render('cascader', [ 'value' => $value, 'name' => $name, 'selected' => $selected, 'items' => Json::encode($this->items), 'boxId' => $this->boxId, 'multiple' => $this->multiple, 'options' => $this->options, 'changeOnSelect' => $this->changeOnSelect, 'dynamicLoading' => $this->dynamicLoading, ]); } /** * 注册资源 */ protected function registerClientScript() { $view = $this->getView(); $view->registerJs(<<