/** * 数据选项值树形处理,主要把选项值转换为树形结构 */ public function itemsHandle() { $this->items = Tree::getTreeList($this->items, $this->pkAttrName, $this->parentAttrName); $items = []; foreach ($this->items as $item) { $items[$item[$this->pkAttrName]] = (isset($item['_spacer']) ? $item['_spacer'] : '') . $item[$this->showAttrName]; } if ($this->topItem !== null) { $items = $this->topItem + $items; } $this->items = $items; }
/** * 取得包含按照等级排列的含有等级前缀角色数组 * * @return array */ public static function getTreeList() { $roles = static::find()->joinWith('parentItem')->where(['type' => Item::TYPE_ROLE])->orderBy('created_at DESC')->asArray()->all(); foreach ($roles as &$role) { $role['parent'] = isset($role['parentItem']) ? $role['parentItem']['parent'] : ''; } $roles = Tree::getTreeList($roles, 'name', 'parent', '_child', ''); foreach ($roles as &$role) { $role['_spacer'] = isset($role['_spacer']) ? str_replace(' ', ' ', $role['_spacer']) : ''; } return $roles; }
/** * 树形处理 * * 这里主要是给列表AR模型的显示属性加上树形等级前缀 */ public function treeHandle() { $tree = $this->_treeBehavior; $arrayModels = ArrayHelper::toArray($this->models); $this->models = ArrayHelper::index($this->models, $tree->pkAttrName); $arrayModels = Tree::getTreeList($arrayModels, $tree->pkAttrName); $showAttrName = $tree->showAttrName; foreach ($arrayModels as &$model) { $spacer = isset($model['_spacer']) ? str_replace(' ', ' ', $model['_spacer']) : ''; $model[$showAttrName] = $spacer . $model[$showAttrName]; } $this->models = $arrayModels; }