Как видно из скриншота, категории Wallpapers, Иллюстации и Разное являются подкатегориями категории Вдохновение. Мне необходимо сделать что-то вроде того же. Пока сделал это через relations и рекурсивную функцию:
Код: Выделить всё
/**
* Lists all Category models.
* @return mixed
*/
public function actionIndex()
{
return $this->render('index', [
'categories' => $this->_getCategoriesList(true, Yii::$app->params['category.list.indent']),
]);
}
/**
* Получить список всех категорий
*/
protected function _getCategoriesList($withModels = false, $indent)
{
if (!$withModels) {
$list = [
'' => Yii::t('app', '== Не выбрано =='),
];
} else {
$list = [];
}
$rootCategories = Category::find()->where(['parent' => null])->all();
foreach ($rootCategories as $cat) {
if ($withModels) {
$list[$cat->id] = $cat;
} else {
$list[$cat->id] = $cat->title;
}
$list = $list + $this->_buildCategoriesListLevel($cat, $withModels, $indent);
}
return $list;
}
/**
* Получает один уровень списка категорий. Рекурсия.
*/
protected function _buildCategoriesListLevel($rootCategory, $withModels, $indent)
{
$list = [];
foreach ($rootCategory->childrenCategories as $cat) {
if ($withModels) {
$cat->title = $indent . $cat->title;
$list[$cat->id] = $cat;
} else {
$list[$cat->id] = $indent . $cat->title;
}
if (count($cat->childrenCategories) > 0) {
$sublist = $this->_buildCategoriesListLevel($cat, $withModels, $indent);
foreach ($sublist as $k => $v) {
if ($withModels) {
$v->title = $indent . $v->title;
$list[$k] = $v;
} else {
$list[$k] = $indent . $v;
}
}
}
}
return $list;
}
Код: Выделить всё
<?php if (count($categories)): ?>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th width="75"><?= reset($categories)->getAttributeLabel('id') ?></th>
<th><?= reset($categories)->getAttributeLabel('title') ?></th>
<th width="100"> </th>
</tr>
</thead>
<tbody>
<?php foreach ($categories as $cat): ?>
<tr>
<td><?= $cat->id ?></td>
<td><?= $cat->title ?></td>
<td align="center">
<a href="<?= Url::to(['category/view', 'id' => $cat->id]) ?>"><span class="glyphicon glyphicon-eye-open"></span></a>
<a href="<?= Url::to(['category/update', 'id' => $cat->id]) ?>"><span class="glyphicon glyphicon-pencil"></span></a>
<a href="<?= Url::to(['category/delete', 'id' => $cat->id]) ?>" data-method="post"
data-confirm="<?= \Yii::t('app', 'Вы уверены, что хотите удалить эту категорию?') ?>">
<span class="glyphicon glyphicon-trash"></span>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p class="bg-info"><?= Yii::t('app', 'Ничего не найдено.') ?></p>
<?php endif; ?>
Но это не совсем хорошо, т.к. пропадает возможности GridView: сортировка, фильтрация и пагинация. При сортировке, естественно, будет обычный вывод с учетом сортировки, но в качестве сортировки "по-умолчанию" хотелось бы именно иерархический вид.
Собственно вопрос: как мне реализовать все это при помощи GridView? Заранее благодарю за помощь!