讨论数量:
应该是用的递归,先把所有数据查出来
protected function buildSelectOptions(array $nodes = [], $parentId = 0, $prefix = '', $space = ' ')
{
$d = '├─';
$prefix = $prefix ?: $d.$space;
$options = [];
if (empty($nodes)) {
$nodes = $this->allNodes()->toArray();
}
foreach ($nodes as $index => $node) {
if ($node[$this->getParentColumn()] == $parentId) {
$currentPrefix = $this->hasNextSibling($nodes, $node[$this->getParentColumn()], $index) ? $prefix : str_replace($d, '└─', $prefix);
$node[$this->getTitleColumn()] = $currentPrefix.$space.$node[$this->getTitleColumn()];
$childrenPrefix = str_replace($d, str_repeat($space, 6), $prefix).$d.str_replace([$d, $space], '', $prefix);
$children = $this->buildSelectOptions($nodes, $node[$this->getKeyName()], $childrenPrefix);
$options[$node[$this->getKeyName()]] = $node[$this->getTitleColumn()];
if ($children) {
$options += $children;
}
}
}
return $options;
}
推荐文章: